How to Display an Excel Page Number in a Cell Using VBA
Question details
The user wants to display the current printed page number and the total number of workbook pages inside a specific worksheet cell instead of using the standard header or footer.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Setting up a customized printable dashboard, invoice, or report where page numbers must be dynamically visible within the spreadsheet grid.
- Observed behavior
- Excel does not have a built-in cell formula for page numbers because page counts depend on dynamic print areas, manual page breaks, and scaling settings.
Ensure your worksheet's print area, scaling, and page orientation are fully configured before proceeding, as page number calculations depend entirely on your final print settings.
Use a Custom VBA Macro to Calculate Page Numbers
Since Excel lacks a built-in formula for this feature, you can use a custom VBA procedure that calculates page breaks relative to your target cell's position.
Calculating the page number of a specific cell requires evaluating both horizontal and vertical page breaks. A VBA macro can count the page breaks above and to the left of the active cell to determine which printed page it will fall on.
Because these calculations are complex and specific to individual workbooks, this method requires careful customization and testing.
Navigate to the Developer tab and click 'Visual Basic', or press ALT + F11 on your keyboard to open the editor.
Click 'Insert' in the top menu and select 'Module' to create a blank workspace for your custom function.
Paste a VBA script designed to count ActiveSheet.HPageBreaks and ActiveSheet.VPageBreaks relative to the target cell's row and column position.
Return to your Excel worksheet, select the cell where you want the page number displayed, and type your newly created custom function (e.g., =GetPageNumber()) into the formula bar.

Use WPS Spreadsheet for Advanced Formatting and VBA
WPS Office Spreadsheet fully supports VBA macros, allowing you to create custom functions for displaying page numbers in cells. It also offers a highly intuitive Page Layout view to make managing print areas and manual page breaks effortless.
- 1. Open Your Workbook in WPS Office: Download and install WPS Office, then open your macro-enabled spreadsheet in WPS Spreadsheet.
- 2. Access the VBA Editor: Go to the 'Developer' tab and open the VBA Editor to implement your custom page number script.
- 3. Adjust Print Settings: Use the 'Page Layout' tab to precisely define your print areas and manage page breaks.
- 4. Apply the Formula: Type your custom function into the desired cell and use 'Print Preview' to verify the correct page number formatting.

Frequently Asked Questions
Why isn't there a built-in Excel formula for page numbers?
Page numbers are determined during the printing process based on paper size, margins, scaling, and print areas. Since cell formulas calculate data values independent of print layouts, Excel natively restricts page number functionality to headers and footers.
Can I use the CELL function to find the page number?
No. The built-in =CELL() function in Excel can return information like formatting, location, or the contents of a cell, but it does not contain a parameter to retrieve printed page numbers.
Will the VBA page number update automatically if I add rows?
Custom VBA functions do not always trigger automatically when print boundaries change. You may need to press F9 to manually recalculate the worksheet or add an 'Application.Volatile' statement to your VBA code to force updates.
What is the easiest alternative to putting page numbers in a cell?
The most reliable and universally supported method is to use the built-in Header & Footer tool. Navigate to the Insert tab, select Header & Footer, and insert the Page Number element to automatically display accurate numbers on printed sheets.




