Changing Section Headers

Written by Allen Wyatt (last updated March 14, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

When working with large worksheets, it is not unusual to add subtotals so that you can group information in the worksheet in some logical manner. (The Subtotal tool is on the Data tab of the ribbon in the Outline group.) When adding subtotals, you can specify that Excel start each group on a brand-new page. This is very handy for all types of reporting in Excel.

If you start each group or subtotal section on a new page, you may wonder if there is a way to create custom headers that print differently for each section, similar to what you can do with different sections in a Word document. Unfortunately, there is no way to do this in Excel. You can, however, create a macro that iteratively changes the heading and prints each group of a worksheet. Consider the following macro:

Sub ChangeSectionHeads()
    Dim c As Range, rngSection As Range
    Dim cFirst As Range, cLast As Range
    Dim rowLast As Long, colLast As Integer
    Dim r As Long, iSection As Integer
    Dim iCopies As Variant
    Dim strCH As String

    Set c = Range("A1").SpecialCells(xlCellTypeLastCell)
    rowLast = c.Row
    colLast = c.Column

    iCopies = InputBox( _
        "Number of Copies", "Changing Section Headers", 1)

    If iCopies = "" Then Exit Sub

    Set cFirst = Range("A1")     ' initialization start cell
    For r = 2 To rowLast    ' from first row to last row
        If ActiveSheet.Rows(r).PageBreak = xlPageBreakManual Then
            Set cLast = Cells(r - 1, colLast)
            Set rngSection = Range(cFirst, cLast)

            iSection = iSection + 1
            Select Case iSection
            '   substitute your CenterSection Header data ...
                Case 1: strCH = "Section 1"
                Case 2: strCH = "Section 2"
            '   etc
            '   Case n: strCH = "Section n"
            End Select

            ActiveSheet.PageSetup.CenterHeader = strCH

            rngSection.PrintOut _
                Copies:=iCopies, Collate:=True

            Set cFirst = Cells(r, 1)
        End If
    Next r

'   Last Section ++++++++++++++++++++++++++++
    Set rngSection = Range(cFirst, c)

    iSection = iSection + 1
'   substitute your Center Header data ...
    strCH = "Last Section ..."

    ActiveSheet.PageSetup.CenterHeader = strCH

    rngSection.PrintOut _
        Copies:=iCopies, Collate:=True
End Sub

This macro is a good start toward accomplishing what you want to do. It starts by asking you how many copies you want to print of each section, and then it starts to go through each row to see if there is a page break before that row.

The actual row checking is done by looking at the PageBreak property of each row. This property is normally set to xlPageBreakNone, but when you use the Subtotals feature of Excel, any row that has a page break before it has this property set to xlPageBreakManual. This is the same setting that would occur if you manually placed page breaks in your worksheet.

If the macro detects that a row has a page break before it, then the rngSection range is set equal to the rows in the previous group. Also, the Select Case structure is used to set the different headings used for the different sections of the worksheet. This heading is then placed in the center position of the header, and the range specified by rngSection is printed.

After stepping through all the groups in the worksheet, the final group (which does not end with a page break) is printed.

In order to use this macro, all you need to do is specify within the Select Case structure the different headings you want for each section of the worksheet. You can also, if desired, change where the heading is placed in the header. All you need to do is change the CenterHeader property to LeftHeader or RightHeader. You can also use LeftFooter, CenterFooter, and RightFooter, if desired.

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 (9867) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

Setting a Transparent Color for an Image

Want to "see through" an image you place on a worksheet? You can do so by using the steps in this tip.

Discover More

Returning an ANSI Value

Need to know the character value of the first character in a string? It's easy to do, without using a macro, by using the ...

Discover More

Understanding Shortcuts

One way of optimizing your user interface is through the use of shortcuts. This tip provides a brief overview of what ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Moving Part of a Footer Down a Line

Setting up a single footer line for your printouts is fairly easy. If you want to move part of the footer down a line so ...

Discover More

Ampersands in Headers and Footers

Add an ampersand to the text in a header or footer and you may be surprised that the ampersand disappears on your ...

Discover More

Creating Long Page Footers

Ever wish that you could create nice, long footers that appear at the bottom of each page when you print your worksheet? ...

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 8 - 5?

2020-09-17 10:18:42

J. Woolley

@David Czuba
I suppose you already know how to manually add a sheet name to the printed header or footer:
1. Ctrl+P to open Print dialog, then click Page Setup.
2. In Page Setup dialog, click Header/Footer tab.
3. Click Custom Header... (or Footer...), then pick a section.
4. Click the Insert Sheet Name tool (or type &[Tab]).
This setting will be saved with the workbook.


2020-09-16 17:29:40

David Czuba

Another useful macro would look at the worksheet Tab name and set the Header or Feeder (Right, Center, or Left) to that specific Tab name. Would anyone like to submit something VBA in that line?


This Site

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.

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.