VBA code to Freeze/Unfreeze Panes based on a Cell Value

lilydias

New Member
Joined
Jul 17, 2018
Messages
2
Hi,

I have a sheet where it would be handy to have/not have frozen panes depending on how you're filling it out. Other people without excel experience will also be using this sheet, though.

I have a cell (x) with data validation that has two options: Freeze Panes/Unfreeze Panes. I'd like to insert a VBA code where if cell X equals "Freeze Panes", that rows 1 to 16 will be frozen. And if cell X says "Unfreeze Panes", that no rows will be frozen.

Anyone?

Thank you very much!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi Lilydias.
Pop this into a module:
Code:
Sub FreezePanes()

    With ActiveWindow
    .SplitRow = 16
    .FreezePanes = True
    End With
    
End Sub

To figure out how to do this, I recorded a macro of me splitting and freezing the panes, then looked at the code and deleted whatever I felt I didn't need.

Hope that helps!
 
Upvote 0
In a module, I put this code:

Code:
Sub FreezePanes()


    With ActiveWindow
        .SplitRow = 16
        .FreezePanes = True
    End With
    
End Sub


Sub unFreezePanes()


    With ActiveWindow
        .FreezePanes = False
        ActiveWindow.SplitRow = 0
    End With
    
End Sub
[\CODE]

Then for it to take effect, pop this into your Sheet code:
[CODE]


Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng As Range
        Set rng = Target.Parent.Range("F2")
            If Target.Count > 1 Then Exit Sub
            If Intersect(Target, rng) = "Freeze" Then
            Call FreezePanes
            Else
            Call unFreezePanes
            End If
            
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top