Code for page number

LearnMeExcel

Well-known Member
Joined
Aug 11, 2009
Messages
746
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all
i have 14 sheets in my workbook
i want to make code to put page number for every sheets in WB
when i use Footer the sheets Number reset from sheet to sheet
i want to start from first sheet and end in last sheet in WB
some worksheet in my wb more than one page

thanx in advanced
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try either of the below they loops through all the sheets and sets the first page to the count of the previous sheets' pages. I found two methods to count the number of page that will be printed reading this ozgrid page (link). The two subs below differ only on how the pages are counted.

The first has to activate the worksheet to get a count but it works better for non rectangular print ranges.
Code:
Sub SetFirstPageNumber()
    Dim ws As Worksheet
    Dim shtPgs As Integer
    Dim shtFirstPg As Integer
    
    shtFirstPg = 1
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.PageSetup.FirstPageNumber = shtFirstPg
        ws.Activate
        shtPgs = ExecuteExcel4Macro("Get.Document(50)")
        shtFirstPg = shtFirstPg + shtPgs
    Next ws
End Sub

This doesn't have to activate all the sheets but the print ranges have to be rectangular i.e. 2x3 4x6.
Code:
Sub SetFirstPageNumber2()
    Dim ws As Worksheet
    
    Dim shtPgs As Integer
    Dim shtFirstPg As Integer
    
    shtFirstPg = 1
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.PageSetup.FirstPageNumber = shtFirstPg
        '// Count the number of page breaks to determine the number of sheets
        shtPgs = (ws.HPageBreaks.Count + 1) * (ws.VPageBreaks.Count + 1)
        shtFirstPg = shtFirstPg + shtPgs
    Next ws
End Sub

EDIT:
This works with the &[Page] in the footer
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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