Excel Guides

Page Numbers in VBA in Excel

There are a few different ways that you can add page numbers to your VBA code in Excel. The first way is to use the PageSetup property. This will allow you to add page numbers as well as other information, such as the date and time. To use this property, you need to set it up first:

Dim pagenum As Integer

With ActiveSheet.PageSetup
    .CenterHeader = "&P"
    .LeftHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .RightFooter = ""
    .CenterFooter = ""
    .Orientation = xlPortrait
    .Draft = False
    .PaperSize = xlPaperLetter
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = False

   'Set the header and footer margins in inches. 

   .HeaderMargin = 0.3  'One inch is equal to 2.54 centimeters. 

   .FooterMargin = 0.2

   'Set the worksheet margins in inches. 

   .LeftMargin = 0.7

   .RightMargin = 0.7

   .TopMargin = 1

   .BottomMargin = 1

   'Set the number of pages wide and high that you want to print. 

   pagenum = InputBox("Enter the number of pages wide.")

   If pagenum > 0 Then _ 

      .ZoomColumnsWidthToPages pagenum _ 

      Else _ 

      MsgBox "You must enter a positive number.": Exit Sub 

   End If 

   pagenum=InputBox("Enter the number of pages high.") 

   If pagenum > 0 Then _ 

      ZoomRowsHeightToPages pagenum _ 

      Else _ 

      MsgBox "You must enter a positive number.": Exit Sub 

   End If 
End With

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.