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.
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.
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.
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.
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.
The page number will appear on the cell.
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.
- 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.
- 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.
- 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.
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.
- 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
- How to Print Page Number in Excel
- How Start Page Numbers at Different Number in Excel
- How to Remove Page Number from Page Break Preview in Excel
- How to Insert Page Number Using VBA in Excel
<< Go Back to Page Number | Page Setup | Print in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
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.
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.