-
AutoFit Columns
Hi
I have used the macro recorder code below
[vba]
Sub AutoFitSheets()
'
' AutoFitSheets Macro
'
'
Sheets("Sheet2").Select
Columns("A:K").Select
Selection.Columns.AutoFit
Sheets("Sheet3").Select
Columns("A:K").Select
Selection.Columns.AutoFit
Sheets("Sheet4").Select
Columns("A:K").Select
Selection.Columns.AutoFit
Sheets("Sheet5").Select
Columns("A:K").Select
Selection.Columns.AutoFit
Sheets("Sheet6").Select
Columns("A:K").Select
Selection.Columns.AutoFit
End
Sub
[/vba]
my question is that I really don't know how many sheets will be in the workbook it could be 2 Sheets to a maximum of 25 Sheets is it possible to just have Autofit run on all sheets within the workbook excluding Sheet1 which is the master sheet for the workbook.
Regards
Sooty 8
-
[VBA]Sub AutoFitAllColsAtoK()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Range("A1:K1").Columns.AutoFit
Next ws
End Sub[/VBA]
-
Hi Kenneth
Thanks for the reply, I really need to exclude sheet 1 from also running the AutoFit because of the format of that sheet the column width's are set to accomodate more data - is this possible.
Regards
Sooty8
-
[VBA]Sub AutoFitAllColsAtoK()
Dim ws As Worksheet
For Each ws In Worksheets
If Not ws.Name = "Sheet1" Then ws.Range("A1:K1").Columns.AutoFit
Next ws
End Sub[/VBA]
-
Hi Kenneth
Many thanks works spot on.
Regards
Sooty8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules