Written by Allen Wyatt (last updated June 21, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365
David is administering an election for a professional society and the roster of eligible voters is a worksheet. There are approximately 1,200 eligible voters, so the printout is over twenty pages long. David has a footer with the page number—which is helpful—but it would be great if he could have, in the header of each page, the first name on the page and the last name on the page.
In Excel there is no native way to do this. It is a relative snap to do in Word, however, so one solution is to paste the sorted names into a Word document and then add the desired header that shows the names. (In Word you would format the last names using a unique style of your choosing and then use the STYLEREF field in the page header to insert names.) While this can work, it becomes a pain to make sure that the Word version of the list is always in sync with the Excel version of the list, and vice-versa.
If you decide you want to keep a single version of the voter list in Excel, the best way to approach the problem is to use a macro to insert the first and last names in the header. The code for such a macro, obviously, would need to be tailored to the layout of the data in your worksheet. The following macro assumes that the names are in columns A through C, with the last names (the ones you want to use for the headers) are in column C.
Sub PrintNamesInHeader() Dim iPages As Integer Dim iPage As Integer Dim iHorPgs As Integer Dim iHP As Integer Dim iHPNext As Integer Dim iCol As Integer Dim iColLast As Integer Dim lRow As Long Dim lRowLast As Long Dim sPrtArea As String iCol = 1 'Col A iColLast = 3 'Col C With ActiveSheet iPages = ExecuteExcel4Macro("Get.Document(50)") iHorPgs = .HPageBreaks.Count + 1 sPrtArea = .PageSetup.PrintArea For iPage = 1 To iPages iHP = ((iPage - 1) Mod iHorPgs) iHPNext = iHP + 1 If iHP = 0 Then If sPrtArea = "" Then lRow = 1 Else lRow = .Range(sPrtArea).Cells(1).Row End If Else lRow = .HPageBreaks(iHP).Location.Row End If If iHPNext > .HPageBreaks.Count Then lRowLast = .Cells(lRow, iColLast).End(xlDown).Row Else lRowLast = .HPageBreaks(iHPNext).Location.Row - 1 End If .PageSetup.LeftHeader = .Cells(lRow, iCol).Value & _ " - " & .Cells(lRowLast, iColLast) .PrintOut From:=iPage, To:=iPage, preview:=True Next End With End Sub
When you run the macro, it steps through each page of the worksheet. The headers are set for the page, then the single page is printed, and then the next page is examined and processed.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9543) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: First and Last Names in a Page Header.
Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!
How do you want your page numbers to appear on your printed worksheets? Chances are good that you want them to be ...
Discover MoreAdd an ampersand to the text in a header or footer and you may be surprised that the ampersand disappears on your ...
Discover MoreNeed to copy headers and footers from one worksheet to another? How about from one workbook to another? Here are some ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-06-21 15:51:51
J. Woolley
Re. the Tip's macro, to accurately determine both the HPageBreaks collection and the number of pages required to print the active sheet, first select Print Preview (Ctrl+P) or View > Page Layout or Page Break Preview. To be certain, add this statement to the beginning of the macro:
ActiveWindow.View = xlPageBreakPreview
ExecuteExcel4Macro is obsolete; therefore, replace this statement
        iPages = ExecuteExcel4Macro("Get.Document(50)")
with the following:
        iPages = .PageSetup.Pages.Count
To print all the pages without manual intervention, modify this statement
            .PrintOut From:=iPage, To:=iPage, preview:=True
as follows:
            .PrintOut From:=iPage, To:=iPage
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments