How to Insert Page Number in Excel Cell Not in Header

Method 1 – Inserting Page Number in Excel Cell

Step 1: Open VBA Window

VBA has its own separate window. You must insert the code in this window, too.

To open the VBA window, click the Developers tab on your ribbon and select Visual Basic from the Code group.

insert page number in excel cell not in header

Step 2: Insert Module

VBA modules hold the code in the Visual Basic Editor and have a .bcf file extension. You can create or edit one through the VBA editor window.

To insert a module for the code, go to the Insert tab on the VBA editor and click on Module from the drop-down menu.

insert page number in excel cell not in header

A new module will be created.

Step 3: Insert VBA Code

Select the module if it hasn’t already been selected. Write down the following code in it.

Sub Page_Count()
    Dim xVCount As Integer
    Dim xHCount As Integer
    Dim xVBreak As VPageBreak
    Dim xHBreak As HPageBreak
    Dim xNumPage As Integer

    xHCount = 1
    xVCount = 1

    If ActiveSheet.PageSetup.Order = xlDownThenOver Then
        xHCount = ActiveSheet.HPageBreaks.Count + 1
    Else
        xVCount = ActiveSheet.VPageBreaks.Count + 1
    End If

    xNumPage = 1

    For Each xVBreak In ActiveSheet.VPageBreaks
        If xVBreak.Location.Column > ActiveCell.Column Then Exit For
        xNumPage = xNumPage + xHCount
    Next

    For Each xHBreak In ActiveSheet.HPageBreaks
        If xHBreak.Location.Row > ActiveCell.Row Then Exit For
        xNumPage = xNumPage + xVCount
    Next

    ActiveCell = "Page " & xNumPage & " of " & Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")

End Sub

Close the VBA window.

Step 4: Turn on Page Layout View

After you have inserted the code, turn on the page layout view before running the code.

insert page number in excel cell not in header

The sheet will look like this.

Step 5: Run VBA Code

Select the cell where you want to put the page number in. We selected cell F34 for this.

insert page number in excel cell not in header

Go to the Developer tab again. Select Macros from the Code group.

Select the Macro name you have just entered. The macro name was Page_Count.

Click on Run.

insert page number in excel cell not in header

The page number will appear on the cell.

insert page number in excel cell not in header


Method 2 – Inserting Page Number in Footer

2.1 Inserting Page Number from Page Layout View

Follow these steps to insert page numbers in Excel footers using the Page Layout view feature.

Steps:

  • Go to the View tab on your ribbon.
  • Select Page Layout from the Workbook Views group.

insert page number in excel cell not in header

  • The dataset will look like this.

  • Select the place on the footer where you want to insert your page number.

  • Tthe Header & Footer tab will appear on the ribbon. Select Page Number from the Header & Footer Elements.

The footer will now look like this.

insert page number in excel cell not in header

  • Click on another cell to have the page number in the Excel footer.


2.2 Inserting Page Number Using Page Setup Dialog Box

Steps:

  • Go to the Page Layout
  • Select the dialog box launcher on the bottom right of the Page Setup group, as the figure below.

insert page number in excel cell not in header

  • The Page Setup box will appear. Now select the Header/Footer tab in it.
  • Select the style of how you want your page number to show on footers from the drop-down of the Footer section.

  • Click on OK.
  • If you are not in the Page Layout view, go to the View tab and select Page Layout from the Workbook Views.

This will also insert the page number in the Excel footer for all pages.

insert page number in excel cell not in header


2.3 Inserting Page Number from Header & Footer Command

Steps:

  • Go to the Insert tab on your ribbon.
  • Go to the Text group.
  • Select Header & Footer.

  • The Page Layout view will automatically pop up.

  • Select the footer position where you want to insert the page number.

  • The Header & Footer tab will appear on the ribbon.
  • Select Page Number from the Header & Footer Elements from the tab.

It will look like this.

insert page number in excel cell not in header

  • Click on a cell on the spreadsheet and you will see the page number on the Excel footer.


Download Practice Workbook

You can download the workbook used for the demonstration from the download link below.


Related Articles


<< Go Back to Page Number  | Page Setup | Print in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Abrar-ur-Rahman Niloy
Abrar-ur-Rahman Niloy

Abrar-ur-Rahman Niloy, holding a B.Sc. in Naval Architecture and Marine Engineering, has contributed to Exceldemy for nearly 1.5 years. As a leader in Excel, VBA, and Content Development teams, he authored 114+ articles and assisted the Exceldemy forum. Presently, as a project writer, he prioritizes stepping out of his comfort zone, aiming for constant technical improvement. Niloy's interests encompass Excel & VBA, Pivot Table, Power Query, Python, Data Analysis, and Machine Learning libraries, showcasing his commitment to diverse... Read Full Bio

2 Comments
  1. Hi, I followed all the instructions; however, the numbers are not sequential when you want to print them. In my case, the cell where I run the VBA displays “Page 1 of 9,” and the last page displays the same, rather than “Page 9 of 9.” I don’t want to use the header and footer because I only recreated the form from Word to Excel.

    Any help on this matter is highly appreciated.

    Thank you. 

    • Dear CHARLES DEL MAR,
      Thank you very much for reading our articles. You want to add the page number in the cell following the method Inserting Page Number in Excel Cell. You mentioned that every time you run the code and get “Page 1 of 9”, even in the last cell where it is supposed to show “Page 9 of 9”. Based on your complaint, we checked all the steps and run the VBA code again. We get an accurate result. See the image below.
      Insert Page Number in Cell Not in Header Solution

      I think you need to check the Status Bar at the bottom of the sheet, where the page number shows. We get the same result showing in the Status Bar after running the VBA code.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo