Home » excel » Excel VBA: Unfreezing panes of a workbook object's sheet?

Excel VBA: Unfreezing panes of a workbook object's sheet?

Posted by: admin April 23, 2020 Leave a comment

Questions:

Normally the code to unfreeze panes is

ActiveWindow.FreezePanes = False

But say I have sourceBook of type Workbook and a sheetName of type String. For instance

sourceBook.sheets(sheetName)

How do I unfreeze the panes on that workbook’s sheet without invoking things like ActiveWindow?

How to&Answers:

This will remove FreezePanes from every sheet for every window for the given workbook, it also makes sure the same worksheet is active as was active before running the code:

Dim w As Window
Dim activews As Worksheet, ws As Worksheet
For Each w In sourceBook.Windows
    w.Activate
    If activews Is Nothing Then
        Set activews = w.ActiveSheet
    End If
    For Each wsv In w.SheetViews
        wsv.Sheet.Activate
        w.FreezePanes = False
    Next
    activews.Activate
    set activews = nothing
Next

As Rick indicated, you cannot unfreeze panes without activating the window. But you don’t have to call ActiveWindow.

Answer:

Unfortunately Freeze Panes is a method of a window object, the sheet on which you want to apply it needs to be the activesheet.
This means that you will have to select the cell on which you would like to freeze the panes on the active window only.

Answer:

Add the code to sheet’s activate event (for e.g. this is the code under Sheet1)

Option Explicit
Private Sub Worksheet_Activate()
    ActiveWindow.FreezePanes = False
End Sub