Looping though sheets in workbook

  • Thread starter Thread starter KHogwood-Thompson
  • Start date Start date
K

KHogwood-Thompson

I have the following code to loop through each sheet in the workbook and
format the columns to Autofit:

For Each Worksheet In ActiveWorkbook.Worksheets
Cells.Select
Selection.EntireColumn.AutoFit
Next

It only seems to Autofit the columns on the first sheet and all the others
remain unchanged except that they have all cells selected. Can anyone advise
as to what I am doing wrong here? or a better way to do this?
 
For Each ws In ActiveWorkbook.Worksheets
ws.Columns.AutoFit
Next

Don't use Worksheet as variable name.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
try this without selections

Sub fitcols()
For Each ws In activeworkbook.worksheets
ws.Columns.AutoFit
Next ws
End Sub
 
Back
Top