VBA to insert sequential page numbers of ONLY visible worksheets

SpencerRichman

New Member
Joined
Feb 15, 2013
Messages
33
Office Version
  1. 2021
Platform
  1. Windows
I have a workbook with 10 or so worksheets. The first worksheet is an 'input' page, and depending on what kinds of information the user inputs, some of the other worksheets either get hidden or unhidden.
At the bottom of the input page I have a button which then prints the workbook to a pdf. When it prints, it is only printing the visible worksheets, not the hidden ones.

I am attempting to figure out how to have page numbers appear on the bottom (footer) of the pages, but the page numbers need to start at 1 for the first VISIBLE worksheet and continue sequentially for all the VISIBLE worksheets so that the final pdf has page numbering that makes sense.

Any thoughts?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Presumably the first worksheet ('input') is visible but you don't want it to be included in the PDF?

Do the sheets already have a page footer?
 
Last edited:
Upvote 0
Presumably the first worksheet ('input') is visible but you don't want it to be included in the PDF?
No, the VBA for the "print to pdf" hides the Input sheet before printing and then unhides it again afterwards. So in between the command to hide the worksheet and the command to print to pdf I need some code to number the pages.
 
Upvote 0
It looks like you missed my question about page footers, which I added to my post just after you replied.

If the sheets already have a page footer then saving the entire workbook as a PDF should save only the visible sheets with the correct page numbering. This macro does that, with .Worksheets(1).Visible = xlSheetHidden hiding the Input sheet.

VBA Code:
Public Sub Create_PDF()

    Dim PDFoutputFile As String
   
    Application.ScreenUpdating = False
   
    With ActiveWorkbook
        .Worksheets(1).Visible = xlSheetHidden
        PDFoutputFile = .Path & "\Pages.pdf"
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFoutputFile, _
            Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        .Worksheets(1).Visible = xlSheetVisible
        .Worksheets(1).Select
    End With
   
    Application.ScreenUpdating = True
   
    MsgBox "Created " & PDFoutputFile
   
End Sub
If they don't have a page footer this macro adds a footer to every visible sheet and saves the workbook as a PDF.
VBA Code:
Public Sub Create_PDF2()

    Dim PDFoutputFile As String
    Dim ws As Worksheet
       
    Application.ScreenUpdating = False
   
    With ActiveWorkbook
        PDFoutputFile = .Path & "\Pages2.pdf"
        .Worksheets(1).Visible = xlSheetHidden
   
        For Each ws In .Worksheets
            If ws.Visible Then
                ActiveWindow.View = xlPageLayoutView
                Application.PrintCommunication = False
                ws.PageSetup.CenterFooter = "&P of &N"
                Application.PrintCommunication = True
                ActiveWindow.View = xlNormalView
            End If
        Next
       
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFoutputFile, _
                                 Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
   
        .Worksheets(1).Visible = xlSheetVisible
        .Worksheets(1).Select
    End With
   
    Application.ScreenUpdating = True
   
    MsgBox "Created " & PDFoutputFile
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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