Yes. Activate the worksheet, select the cell that marks the top-left corner of the scrolling area, then set ActiveWindow.FreezePanes = True. For example, freezing at C4 keeps rows 1–3 and columns A–B visible while the rest of the sheet scrolls.
What Freeze Panes Does
Freeze Panes divides the worksheet view into a stationary area and a scrolling area. The cell you choose is the first cell in the scrolling area; Excel freezes the rows above it and the columns to its left. The chosen cell itself is not frozen.
For example, selecting C4 freezes the first three rows and the first two columns. Selecting A2 freezes only row 1, while selecting B1 freezes only column A. Selecting B2 freezes both row 1 and column A. This makes the feature useful when a large list has headings across the top, identifying values down the left, or both.
Freeze Panes affects the worksheet’s view, not its data. It does not protect cells, prevent edits, or change what is printed. It is also different from hiding rows or columns: frozen rows and columns remain visible in the window as you scroll through the other cells.
VBA to Freeze at a Specific Cell
This macro freezes the view at C4 on a worksheet named Data. It first activates the worksheet, clears any existing frozen panes in the active window, and then sets the split based on the target cell’s row and column numbers.
Sub FreezeAtSpecificCell()
Dim ws As Worksheet
Dim target As Range
' Change the sheet name and target cell as needed.
Set ws = ThisWorkbook.Worksheets("Data")
Set target = ws.Range("C4")
ws.Activate
With ActiveWindow
.FreezePanes = False
.SplitRow = target.Row - 1
.SplitColumn = target.Column - 1
.FreezePanes = True
End With
End Sub
For C4, target.Row - 1 evaluates to 3 and target.Column - 1 evaluates to 2. Excel therefore freezes three rows at the top and two columns at the left. To freeze at another cell, change ws.Range("C4") to the desired address—for example, ws.Range("B2") freezes row 1 and column A.
The code uses ThisWorkbook, which means the workbook containing the VBA code. That is convenient when the macro is stored in the workbook it changes. If the code is in PERSONAL.XLSB or another macro workbook, replace ThisWorkbook with a reference to the workbook that contains the Data sheet, such as Workbooks("Report.xlsx"). The workbook name must match an open workbook.
Why the SplitRow and SplitColumn Values Matter
FreezePanes is a property of Excel’s active window, while SplitRow and SplitColumn specify where the view is divided. Their values are counts, not worksheet addresses: a split row value of 3 means the first three rows stay above the division; a split column value of 2 means the first two columns stay to its left. Setting FreezePanes to True turns that configured division into frozen panes.
This is why the macro activates the target worksheet before working with ActiveWindow. If another workbook or sheet is active, the command may affect the wrong view or fail to find the intended worksheet. The target range can be on the worksheet without being selected because the macro calculates the split from its row and column properties.
For a one-off task, another valid approach is to select the target cell and set ActiveWindow.FreezePanes = True. The explicit split-count approach is more predictable in reusable macros because it derives the boundary directly from the cell address. In either approach, turn off an existing freeze first if the macro is meant to replace it.
How to Choose the Target Cell
Choose the cell immediately below the rows you want to keep visible and immediately to the right of the columns you want to keep visible. These examples show the relationship:
- A2: freeze just the first row, often a table’s header.
- B1: freeze just column A, often a list of names or item identifiers.
- B2: freeze row 1 and column A together.
- D5: freeze rows 1–4 and columns A–C, which can help when a report has several header rows and identifying columns.
Check the actual layout before choosing a cell. If your headings occupy rows 1 through 4, selecting A5 keeps all four rows visible. If the identifying fields occupy columns A through C, selecting D1 freezes those three columns. A target such as D5 combines both choices. Freeze Panes keeps contiguous rows from the top and contiguous columns from the left; it cannot keep an arbitrary, separated set of rows or columns visible.
Running the Macro Safely
On Excel for Microsoft 365 on Windows, open the workbook that contains the code, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste the procedure into the module. Return to Excel, press Alt+F8, select FreezeAtSpecificCell, and choose Run. You can also run a procedure from the Visual Basic Editor.
Save a copy of the workbook before running a macro. VBA actions generally cannot be reversed with Ctrl+Z, and this procedure deliberately replaces the active window’s existing freeze setting. If you want to keep the code in the workbook, save it as an Excel Macro-Enabled Workbook (.xlsm); a standard .xlsx file does not store VBA macros.
Excel for Mac supports VBA macros, though the ribbon and keyboard access to the Visual Basic Editor can differ from Windows. Use the Developer tab’s Visual Basic command where available, and use the View tab’s Freeze Panes menu for the manual equivalent. Excel for the web does not run VBA macros, so run this procedure in the desktop application.
Common Problems and Fixes
- The wrong sheet or window changes: Confirm that the intended workbook and worksheet are active. Freeze Panes is a window-view setting, so a macro should activate the sheet it is meant to change before referencing
ActiveWindow. - The macro cannot find the worksheet: Check the sheet tab name, including spaces, and make sure the worksheet belongs to the workbook referenced in the code. A tab caption is not necessarily the same as a VBA code name.
- The old boundary remains: Unfreeze the current panes before setting the new split. The sample procedure does this with
.FreezePanes = False, then assigns the new split counts. - Nothing appears frozen: Verify that the target is not A1. At A1, both split counts are zero, so there are no rows above or columns to the left to keep stationary. Choose a cell below row 1, to the right of column A, or both.
- The boundary looks misplaced: Remember that the macro counts worksheet rows and columns from the top and left. Hidden rows or columns can make the visible result less intuitive; unhide them temporarily and check the target address.
When you use multiple windows for the same workbook, remember that the active window matters. Each window can present the sheet with its own view. If users open a different window arrangement, verify the freeze setting in that view rather than assuming the currently visible window is the only one involved.
Freeze Panes Compared with Other Options
For a simple header, Excel’s built-in commands may be quicker than a macro: on Windows, choose View > Freeze Panes > Freeze Top Row or Freeze First Column. The Freeze Panes command in the same menu uses the current selection, which is the manual counterpart to targeting a particular cell in VBA. The Windows ribbon Key Tip sequence Alt+W, F, F opens the Freeze Panes command; Key Tips can vary with ribbon customizations.
Use Split when you want separate panes that can scroll independently rather than keeping part of the sheet stationary. Use Freeze Panes when the top rows or left columns should stay in place as you move through the remaining worksheet. If the goal is to repeat headings on printed pages, use the page-layout print-title settings instead; freezing panes is primarily a screen-view feature and does not itself repeat headings in printouts.
Frequently Asked Questions
What cell freezes the top row in VBA?
Use A2 as the target. The row above A2 is row 1, and there are no columns to its left, so only the top row is frozen.
How do I freeze the first column and top row together?
Use B2 as the target. Excel freezes row 1 above it and column A to its left. In the macro, set the target range to ws.Range("B2").
Can I freeze panes at the currently selected cell?
Yes. Activate the intended worksheet, select the cell, and set ActiveWindow.FreezePanes = True. If replacing an existing freeze, set the property to False first. The selected cell becomes the first cell in the scrolling area.
Can I freeze a row in the middle of a worksheet without freezing the rows above it?
No. Freeze Panes always keeps a contiguous block of rows from the top and columns from the left. It does not pin an isolated middle row while allowing the rows above it to scroll independently.
Does Freeze Panes protect cells from editing?
No. It only changes what stays visible while scrolling. To prevent edits, use worksheet protection or another appropriate access-control feature; freezing panes alone provides no protection.
Related
- Excel Page Break Header: Repeat Headings on Every Page
- Excel Page Break Command: Insert, Move, and Remove
- Excel Page Break Character: Meaning and Print Breaks
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Move a Picture in an Excel UserForm with the MouseTo move a picture around an Excel UserForm with the mouse, handle the Image control’s MouseDown and MouseMove…
- Excel VBA List Worksheet Names: Methods and ExamplesTo list worksheet names with Excel VBA, loop through the workbook’s Worksheets collection and read each sheet’s Name…
- Optimize Copy-Paste Formulas in VBATo optimize formula copying in Excel VBA, assign a formula to an entire destination range at once instead…
- Macro Recorder for Windows 11: Options That Actually WorkWindows 11 does not include a universal built-in macro recorder that can record and replay actions across every…
- Excel VBA Array Maximum Size: Limits and Practical TipsThere is no single, guaranteed maximum number of elements for an Excel VBA array: its usable size depends…
- VBA Delete Row and Shift Up: Excel Macro ExamplesTo delete an entire worksheet row with VBA and move the rows below it up, use Rows(rowNumber).Delete, such…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.