Autofit column width of all worksheets in active workbook - an example

D

DataFreakFromUtah

No question here, just a procedure for the archive.

Search criteria: Autofit auto fit width of all columns in an worksheets work sheets
active workbook justify width all column in work book automatically
programmatically macro to resize column width

Sub ColumnsAutoFitAllSheets()
'AutoFit all column widths in all worksheets in workbook

Application.ScreenUpdating = False
For i = 1 To Sheets.Count
Sheets(i).Select
Cells.EntireColumn.AutoFit
Range("A1").Select
Next
Application.ScreenUpdating = True


End Sub
 
A

Anders Silven

DataFreakFromUtah,

A bit shorter and a bit more explicit,

'*****
Sub test()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
wks.Columns.AutoFit
Next
End Sub
'*****

Variants:

For Each wks In ThisWorkbook.Worksheets
For Each wks In ActiveWorkbook.Worksheets
For Each wks In Workbooks("Book1").Worksheets

--

Also, your code will crash if there is a Chart sheet in the workbook.

Regards,
Anders Silven
 

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