Set column width in VBA

Titian

Well-known Member
Joined
Dec 17, 2004
Messages
567
I have 21 worksheets in a workbook and I can alter the zoom ratio with this code

Code:
ActiveWorkbook.Sheets.Select
    ActiveWindow.Zoom = 80

however I also want to change column widths on all worksheets BUT this will not work

Code:
ActiveWorkbook.Sheets.Select
    Sheets("Fred").Activate
    Columns("D:D").EntireColumn.AutoFit
    Columns("F:F").EntireColumn.AutoFit
    Columns("I:I").EntireColumn.AutoFit
    Sheets("Bill").Activate

Where am I going wrong, please?
 
Perhaps like this

Code:
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    With ws
        .Columns("D").AutoFit
        .Columns("F").AutoFit
        .Columns("I").AutoFit
    End With
Next ws
 
Upvote 0
Thanks Peter, that works fine.

My trial and error method clearly means I am lacking knowledge of some fundamental rules as to how this ought to be accomplished.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top