Looping though sheets in workbook

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?
 
B

Bob Phillips

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)
 
D

Don Guillett

try this without selections

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

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