Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Switching Headers in a Frozen Row.

Switching Headers in a Frozen Row

Written by Allen Wyatt (last updated November 24, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

Tim's got some worksheets that have two separate sections (let's call them upper and lower sections) that have slightly different column headers. He's frozen the top row to keep the upper section headers visible while scrolling down, but after scrolling past a certain point Tim ends up looking at lower-section data with upper-section headers still at the top. He'd like to know if there is a way to switch that frozen header row to show the lower section headers when he scrolls down to the point that only lower-section data is showing.

Yes, there is a way to do this, but it involves the use of macros. Before considering a macro-based solution, you may want to consider restructuring your data so that each of your sections are on different worksheets. (From a design perspective, this would be the easiest solution.) If this is not possible, then you need to be looking at macros.

One easy approach is to simply change what is stored in the top row (row 1) of your worksheet, depending on what row is selected. For instance, the following macro will make changes in the top row based on where the active cell is located. If it is before row 40, then one set of headers are stuffed into the first row; if after row 40 then another set of headers are stuffed.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim iBottomData As Integer

    iBottomData = 40

    If ActiveCell.Row < iBottomData Then
        Cells(1, 1).Value = "Last Name"
        Cells(1, 2).Value = "First Name"
        Cells(1, 3).Value = "Address"
        Cells(1, 4).Value = "Balance"
    Else
        Cells(1, 1).Value = "Account"
        Cells(1, 2).Value = "Sales Rep"
        Cells(1, 3).Value = "Status"
        Cells(1, 4).Value = ""
    End If
End Sub

To use the macro, just make sure that you place it in the code window for the worksheet that contains the two data sections. You should also change the value assigned to the iBottomData variable to reflect the row number of where your bottom data section starts.

If you want to actually change the frozen row as you move down the worksheet, then the macro needs to be a bit more robust. Actually, there are two macros that follow (both go, again, in the code window for the worksheet), and they are kicked into action as you change the selected cell and as you right-click on the worksheet.

Private Sub Worksheet_BeforeRightClick(ByVal _
  Target As Range, Cancel As Boolean)
    Application.ScreenUpdating = False
    ActiveWindow.FreezePanes = False
    ActiveWindow.Split = False
    Application.EnableEvents = False
    Application.Goto Cells(1, 1), scroll:=True
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    Application.Goto Cells(Target.Row - 1, _
      Target.Column), scroll:=True
    Application.EnableEvents = True
    On Error Resume Next        'MUST reenable events
    Application.EnableEvents = False
    ActiveCell.Offset(-1, 1 - Target.Column).Select
    ' so the right click menu doesn't popup
    ' only if this is the second header row.
    Cancel = True 
    Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Cells(Target.Row, 1).Value <> "title2" Then Exit Sub
    ActiveWindow.FreezePanes = False
    ActiveWindow.Split = False
    Application.Goto Cells(Target.Row, 1), scroll:=True
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
    On Error Resume Next        'MUST reenable events
    Application.EnableEvents = False
    ActiveCell.Offset(1, 1 - Target.Column).Select
    Application.EnableEvents = True
End Sub

The Worksheet_SelectionChange event handler automatically moves the frozen split to below the second row of headings when your active cell cursor hits that line. This line is detected in the If statement that checks if the first cell in the row contains the text "title2" or not. (Obviouly, this should be changed to reflect what will really be in that first cell.)

The Worksheet_BeforeRightClick event handler moves the frozen split back to the first set of headings but leaves the active cell at the row above the second set of headings.

You should understand that both of the macro solutions presented in this tip assume that you are actually scrolling through the worksheet and changing the selected cell as you go. (In other words, you are pressing the Down Arrow key to do your scrolling.) If you are simply changing what is displayed in the worksheet by using the vertical scroll bar, then the frozen headings won't change because you are not changing the selected cell and the event handlers never trigger.

Creating a more extensive solution would be beyond the scope of this tip because it would involve interfacing with the actual operating system. If you are interested in going this route, however, a good starting place might be this page at Chip Pearson's Web site:

http://www.cpearson.com/excel/DetectScroll.htm

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 (11231) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Switching Headers in a Frozen Row.

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 with Graphics

If the graphics that you insert in your worksheet meet a couple of simple requirements, it is possible to have those ...

Discover More

Creating and Naming a Worksheet Using a Macro

You can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...

Discover More

Copying a Worksheet

Need to make a copy of one of your worksheets? Excel provides a few different ways you can accomplish the task.

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 (ribbon)

Viewing Two Worksheets At Once

If you need to work on two worksheets in the same workbook at the same time, Excel makes this rather easy to do. All you ...

Discover More

Disabling Moving Between Worksheets

If you want someone to not be able to move from one worksheet to another in a workbook, you've got your work cut out for ...

Discover More

Finding a Worksheet to Unhide among Many Hidden Sheets

Trying to track down a single worksheet among many hidden worksheets can be a challenge. This tip examines a few ...

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 two more than 7?

2016-12-15 12:04:16

John

**UPDATE**

Suzie .... using the right-click on the mouse unfreezes the panes enabling you to scroll up without difficulty. I misread Allen's description above of the codes.


2016-12-15 11:46:14

John

I'd like to echo Suzie's comment. I found that a little awkward around it.

I think this macro needs a reverse search ability to possibly store the last row frozen so when the active cell goes back up it would go back to the last header row.

Suzie -- I was able to press F% and select A1 and start scrolling down again without having to unfreeze the panes.


2015-10-23 01:11:07

suzie

Not sure if this wasn't supposed to happen, but once you scroll down past the cell that coverts it to the 2nd header, you can't scroll up anymore! I would expect it to go back to the first header? but it freezes the 2nd header (which in my case was line 77) and then basically didn't let you see anything before that anymore so it just cuts your spreadsheet in half. To get rid of it, you had to unfreeze your panes & then the screens were split, so you had to get rid of that as well, before you could see anything on top again. Which really isn't user friendly if you're sending this spreadsheet to someone who isn't tech savy with Excel. I was super excited to see this macro but I don't think it's quite there yet. :/


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.