For each (sheet) loop only works on activesheet?

storemannequin

Board Regular
Joined
May 29, 2010
Messages
108
Trying to figure out what's wrong here that this recorded macro, when put inside a loop, only works on the activesheet and no others:

Code:
Sub HeaderFooter()
    Application.ScreenUpdating = False
 
    For Each ws In ActiveWorkbook.Worksheets
 
    With ws
 
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$1"
        .PrintTitleColumns = ""
        .LeftHeader = "&""Arial,Bold""&12This" & Chr(10) & "dsf#" & Chr(10) & "_______(dE)"
        .CenterHeader = "&""Arial,Bold""&16CUSTOMER NAME" & Chr(10) & "REPORT NAME&""Arial,Regular""&10" & Chr(10) & "&""Arial,Bold""&12MM/DD/YYYY"
        .RightHeader = "&""Arial,Bold""&12Date Created:" & Chr(10) & "MM/DD/YYYY"
        .LeftFooter = "&F DK"
        .CenterFooter = ""
        .RightFooter = "QTY= Quantity Shipped " & Chr(10) & "Afd= Avd " & Chr(10) & "EXT= Extended Sales"
        .CenterFooter = "Page &P of &N" & Chr(10) & "Proprietary Information"
        .CenterHorizontally = True
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintGridlines = True
    End With
 
    End With
Next ws
 
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
With ActiveSheet.PageSetup

should be:

With .PageSetup

(leave out ActiveSheet)
 
Upvote 0
Try something like this...


Code:
Sub HeaderFooter()

    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        With ws.PageSetup
            .PrintTitleRows = "$1:$1"
            .PrintTitleColumns = ""
            .LeftHeader = "&""Arial,Bold""&12This" & Chr(10) & "dsf#" & Chr(10) & "_______(dE)"
            .CenterHeader = "&""Arial,Bold""&16CUSTOMER NAME" & Chr(10) & "REPORT NAME&""Arial,Regular""&10" & Chr(10) & "&""Arial,Bold""&12MM/DD/YYYY"
            .RightHeader = "&""Arial,Bold""&12Date Created:" & Chr(10) & "MM/DD/YYYY"
            .LeftFooter = "&F DK"
            .CenterFooter = ""
            .RightFooter = "QTY= Quantity Shipped " & Chr(10) & "Afd= Avd " & Chr(10) & "EXT= Extended Sales"
            .CenterFooter = "Page &P of &N" & Chr(10) & "Proprietary Information"
            .CenterHorizontally = True
            .Orientation = xlLandscape
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
            .PrintGridlines = True
        End With
    Next ws

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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