Macro for page numbering

rwallage

New Member
Joined
May 28, 2006
Messages
34
Hi, I get an Excel file from our accounting system, containing several spreadsheets. I have recorded a macro for the page setup to print it all, and it works, apart from page numbering. I run the macro, but when done, there is no page numbering.
This is part of the macro:
With ActiveSheet.PageSetup
.LeftHeader = "&BBalance Sheet&B" & Chr(10) & "Tel Aviv Hilton"
.CenterHeader = ""
.RightHeader = "&D" & Chr(10) & "renewallage" & Chr(10) & "Page "
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = "&P"
The last line is supposed to give the page number. While recording the macro I saw that on screen it actually writes "&[Page]", so I tried that as well. Same result. The macro runs, but afterwards there is no numbering.

What am I doing wrong?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Neither on the preview, nor the print itself. When I go into the Page Setup - Header/Footer the field is empty; as if the macro did not run.
 
Upvote 0
Have you stepped through each line of code, making sure each one executes as expected?
 
Upvote 0
I have re-recorded the macro, and it now works for the first spreadsheet, but not for the next two!
Also, the system supplies a header, which in the macro I shortened. When running the macro, the header returns to the previous setting! Infuriating doesn't begin to describe it...
 
Last edited:
Upvote 0
You will need to run the code for each sheet. The page setup is only for that specific worksheet. Something like this would set the same pagesetup details for each sheet in the workbook:

Code:
Private Sub SetupPages()
Dim sh As Worksheet
    
    For Each sh In ThisWorkbook.Worksheets
        sh.PageSetup.RightFooter = "&P"
    Next
    
End Sub
 
Upvote 0
Or select all the sheets first:

Code:
Sub addHeaderFooter()
Sheets.Select


With ActiveSheet.PageSetup
.LeftHeader = "&BBalance Sheet&B" & Chr(10) & "Tel Aviv Hilton"
.CenterHeader = ""
.RightHeader = "&D" & Chr(10) & "renewallage" & Chr(10) & "Page "
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = "&P"
End With
Sheets(1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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