by Allen Wyatt
(last updated July 18, 2017)
Wendy wants to include page numbers in the header of her worksheet printout, but with a twist—Page 21A on page 1, Page 21B on page 2, Page 21C on page 3, etc. She wonders how to go about creating such a page numbering scheme.
There are a few ways you can go about tackling this problem, all of them involving the use of macros. If you actually want to print all the worksheets in the current workbook and none of those worksheets is over a single page in length (when printed), then the following macro will set the center section of the header as requested:
Sub PageNums1() Dim sheet As Worksheet Dim J As Integer J = 1 On Error Resume Next For Each sheet In Worksheets Sheets(J).PageSetup.CenterHeader = "Page 21" & Chr(64 + J) J = J + 1 Next End Sub
Note that the macro doesn't actually print anything; all it does is to change the header information. If you really only want to print out the current worksheet and that worksheet will require multiple pages on the printout, then the following should work just fine:
Sub PageNums2() Dim X As Integer Dim Y As Integer Dim Z As Integer Z = 1 For X = 1 To ActiveSheet.HPageBreaks.Count + 1 For Y = 1 To ActiveSheet.VPageBreaks.Count + 1 ActiveSheet.PageSetup.CenterHeader = _ "Page 21" & Chr$(64 + Z) Worksheets.PrintOut Z, Z Z = Z + 1 Next Y Next X End Sub
This macro calculates pages based on the position of the horizontal (HPageBreaks) and vertical (VPageBreaks) page breaks on the printout. You could also try just working with the Pages collection, in this manner:
Sub PageNums3() Dim J As Integer For J = 1 To ActiveSheet.PageSetup.Pages.Count ActiveSheet.PageSetup.CenterHeader = "Page 21" & Chr(64 + J) Worksheets.PrintOut J, J Next J End Sub
You should note that regardless of the approach you select, you'll run into problems if the printout requires more than 26 pages.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12548) applies to Microsoft Excel 2007, 2010, and 2013.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
When you print a worksheet, you may want to specify that the printout be done on a particular paper tray in a particular ...Discover More
If a printout of your worksheet requires multiple pages, you may want to specify the order in which Excel prints those pages. ...Discover More
Excel can print your worksheet on just about any paper size you can imagine. How you select the paper size you want used ...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.