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: Changing Section Headers.

Changing Section Headers

by Allen Wyatt
(last updated June 23, 2018)

4

When working with large worksheets, it is not unusual to add subtotals (Data | Subtotals) so that you can group information in the worksheet in some logical manner. 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 and 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 (2821) 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: Changing Section Headers.

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

Excel 2007 Filters and Filtering (Table of Contents)

Excel provides two ways to filter your data so that only what you want to see is displayed. Discover how filtering works ...

Discover More

Including Headers and Footers when Selecting All

If you are creating a macro that, in the course of processing your document, needs to update all the fields in the ...

Discover More

Pasting Numeric Values in Other Programs

When you paste information from Excel into other programs, you may get more than you actually want. It is not unusual for ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Multiple Line Headers and Footers

When working with headers and footers in a macro, you might find this tip helpful. It describes how you can create ...

Discover More

Turning Headers On and Off

Normally Excel displays row and column headers in a worksheet. If you prefer, you can turn these navigational aids off ...

Discover More

Putting Headers and Footers On Multiple Worksheets

You can easily create headers and footers for multiple worksheets by working with a selection set of the worksheets you ...

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}] 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 five minus 3?

2019-07-25 04:19:31

Spyros

Hi, Miguel
I'm glad it works for you too. Really, I cannot answer your question for sure because I am not a computer geek. In my opinion, since "DisableAcrylicBackgroundOnLogin" in QWord(64-bit) remains inactive, ie seems useless, just delete it and see what happens, or, better, export it (go to file and click on 'export') somewhere, eg. your Documents, and import it again, by right clicking it, in case you find out that something in your computer does not function as expected.
Have a nice day


2019-07-24 10:14:28

Miguel

Hi, Spyros: Thank you very much for your Tip. In fact, disabling 'transparency effects' returns colorful images when starting Windows 10, latest version. On the other hand, "DisableAcrylicBackgroundOnLogin" in QWord(64-bit) field with value 1, added previously, is still in the Windows Registry: Is it necessary or must be deleted? Seems doesn't take any effect...

As always, thanks again.


2019-07-21 09:58:45

Spyros

There is another way to turn off the blurring effect, if you are not familiar with tweaking the registry. Right click on your desktop and choose 'personalize'. Click on 'colors', on the left pane, scroll down and switch off 'transparency effects', on the right pane. That's it, it works fine for me.


2019-07-16 09:05:25

Miguel

Hi Eric, how are you? Fine? I hope so...

Thank you very much for your kind answer through this Windows 10 tip. Well, I followed your instructions, but when selecting DWORD (32-bit) Value, no editing tasks were possible in this 'field'; editing tasks weren't enabled if DWORD (32-bit) Value is selected. So, due to my OS System is Windows 10 Home Edition 64 Bits, version 1903 18362.239, I selected QWORD (64-bit) Value instead, renamed to "DisableAcrylicBackgroundOnLogin" (without quotes) and press Enter. After that, I double-clicked the new "DisableAcrylicBackgroundOnLogin" (without quotes) just created and changed 0 to 1 in the Value data field. Finally, I Closed the Registry Editor app. However, when restarting PC system, blurred image is still displayed again... ¡Nothing changed!

In any case, thanks again, Eric, for your attention.


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.