Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: First and Last Names in a Page Header.

First and Last Names in a Page Header

Written by Allen Wyatt (last updated October 15, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


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. 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 of 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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9542) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: First and Last Names in a Page Header.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Sorting Single-Column Addresses

Got a document that contains a bunch of addresses? If you want to sort the addresses, then you are in for a surprise ...

Discover More

Copying Styles

If you use styles in your documents, you know it can take a good investment of time to get them just the way you want. ...

Discover More

Picking a Workbook Format

Need to share workbook information with a wide number of people? It can be puzzling to figure out which version of Excel ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Header and Footer Background Color

Want to add some color to the printing of your page headers and footers? Your options are limited, as disclosed in this tip.

Discover More

Positioning Headers and Footers

If your workbook is destined for the printed page, you need to be concerned with the layout of that page. Excel allows ...

Discover More

Leading Zeros in Page Numbers

Page numbers in Excel printouts are typically simple counters, without much chance for embellishment. If you want to add ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.