Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Changing Section Headers

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.

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.

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

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

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.