Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Page Numbers in VBA.
by Allen Wyatt
(last updated March 18, 2017)
Steve is looking for a way to determine, in a VBA macro, the number of pages that a worksheet will have, when printed, and the page number on which a particular cell will print. This task is not quite as easy as one would hope, but it can be done.
It seems that the best way to handle this is to use an outmoded (but still available) Excel 4 function to determine the number of total printed pages in a worksheet. Then you can use the HPageBreaks and VPageBreaks collections to figure out where the cell falls in the matrix of pages that will be printed. The following is an example of a macro that utilizes these items:
Sub PageInfo() Dim iPages As Integer Dim iCol As Integer Dim iCols As Integer Dim lRows As Long Dim lRow As Long Dim x As Long Dim y As Long Dim iPage As Integer iPages = ExecuteExcel4Macro("Get.Document(50)") With ActiveSheet y = ActiveCell.Column iCols = .VPageBreaks.Count x = 0 Do x = x + 1 Loop Until x = iCols _ Or y < .VPageBreaks(x).Location.Column iCol = x If y >= .VPageBreaks(x).Location.Column Then iCol = iCol + 1 End If y = ActiveCell.Row lRows = .HPageBreaks.Count x = 0 Do x = x + 1 Loop Until x = lRows _ Or y < .HPageBreaks(x).Location.Row lRow = x If y >= .HPageBreaks(x).Location.Row Then lRow = lRow + 1 End If If .PageSetup.Order = xlDownThenOver Then iPage = (iCol - 1) * (lRows + 1) + lRow Else iPage = (lRow - 1) * (iCols + 1) + iCol End If End With MsgBox "Cell " & ActiveCell.Address & _ " is on " & vbCrLf & "Page " & _ iPage & " of " & iPages & " pages" End Sub
One thing that you should keep in mind with this macro is that the HPageBreaks and VPageBreaks collections are only considered accurate if you are viewing the worksheet in Page Break Preview (View | Page Break Preview). Thus, you'll want to make sure that you are in that mode before selecting a cell and running the macro.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11581) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Page Numbers in VBA.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Logical structures are important in programming, as they allow you to control how the programming statements are ...Discover More
If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...Discover More
When developing a macro, you may want to display on the status bar what the macro is doing. Here's how to use this ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.