Macro to ajust columns!

T

tratliff

I have a workbook with @50 tabs. Each tab is setup the same. I want t
autofit certain columns (F, H, L, M) on all tabs. There are hidden tab
(G, I, J, K) that I do not want to autofit.

please help!!
 
T

Tom Ogilvy

for each sh in Thisworkbook.Worksheets
sh.Range("F:F,H:H,L:M").EntireColumn.Autofit
Next
 
D

DJH

Try this.

Creat a Macro:

Sub AutoFix()
Range("F:F,H:H,L:M").Columns.AutoFit
End Sub

Then for each worksheet:
Private Sub Worksheet_SelectionChange(ByVal Target As
Range)
AutoFit
End Sub
 
S

sp00nix

Sub quickFit()
For Each sht In ActiveWorkbook.Worksheets
sht.Columns("F:F").EntireColumn.AutoFit
sht.Columns("H:H").EntireColumn.AutoFit
sht.Columns("L:L").EntireColumn.AutoFit
sht.Columns("M:M").EntireColumn.AutoFit
Next sht
End Sub



try that
 
D

Don Guillett

try

Sub autofitcols()
for each ws in worksheets
ws.Range("f1,h1,l1,m1").Columns.AutoFit
next
End Sub
 
S

sp00nix

This way is a little more "politically correct" from a programmer's
perspective. Very easy to update without having to changing too many
lines.


Code:
--------------------

Sub AutoFitCertainColumns()

AutoFitCols = Array( _
"F:F", _
"H:H", _
"L:L", _
"M:M" _
)

For Each sht In ActiveWorkbook.Worksheets
For i = 1 To UBound(AutoFitCols)
sht.Columns(AutoFitCols(i)).EntireColumn.AutoFit
Next i
Next sht
End Sub

--------------------
 
G

Guest

Both Don's and Tom's Marcos are good for a one time
event.

But to Auto Fit columns on the fly, it must be tied to an
event for the worksheet. Using their Macros for a
worksheet event would result in reforming the four columms
on the 50 sheet every time the event occurs.
 
T

Tom Ogilvy

There was no talk by the original poster of having the action performed on
Entry, or on the fly as you state - thus requiring an event. If that were
the case, the one solution offered, using the worksheet event, would need to
be entered in 50 sheets and would autofit all 4 columns on each changed of
selection. If on the fly was the requirement, the the workbook level event
would be appropriate and only one column would possibly be changed based on
the change events target property. I never suggested that my code should be
called from an event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top