Formatting Multiple Sheets

Lovelylou79

New Member
Joined
Sep 4, 2017
Messages
37
Hi Again VBA Experts,

I am trying to select all sheets in my workbook after and inclusive of Sheets("ps"), then format each sheet.

I have the following code. It selects all sheets, all sheets widen to 100, but only the first sheet (ps) is then reformatted to Autofit.

Can you please point out where my code is failing. It seems to work during a step through.

Sub FormatMySheets()

Dim i As Long

'Select All sheets
Sheets("ps").Activate
For i = ActiveSheet.Index To Sheets.Count
Sheets(i).Select Replace:=False

Cells.Select

With Selection
.ColumnWidth = 100
.EntireColumn.AutoFit
.EntireRow.AutoFit

End With

Next i

End Sub

Thank you
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Not sure what you mean by "It seems to work during a step through. ", but I think you will have to loop through each sheet for the autofit lines.
 
Upvote 0
.
Just tested your macro here ... it works as designed on my machine.

This macro works as well :

Code:
Option Explicit


Sub FormatMySheets()


Dim i As Long


'Select All sheets
Sheets("Sheet1").Activate
For i = 1 To Sheets.Count  '<-- I changed this line only
Sheets(i).Select Replace:=False


Cells.Select


With Selection
.ColumnWidth = 100
.EntireColumn.AutoFit
.EntireRow.AutoFit


End With


Next i


End Sub
 
Upvote 0
Lovelylou79,

Welcome to the Board.

You might consider the following...

Code:
Sub FormatMySheets()
    Dim i As Long
    Sheets("ps").Activate
    For i = ActiveSheet.Index To Sheets.Count
        With Sheets(i).UsedRange
            .ColumnWidth = 100
            .EntireColumn.AutoFit
            .EntireRow.AutoFit
        End With
    Next i
End Sub

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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