VBA Freeze Panes on Multiple Very Hidden Sheets

nitrammada

Board Regular
Joined
Oct 10, 2018
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone
I have some code that will FreezePanes at cell B7 perfectly, but I'm trying to run this on all of the veryhidden sheets where cell A1 = L2, without me having to open each one and then run the code.
Can any one tell me what I'm doing wrong please? My code is below.

VBA Code:
Sub FreezeL6()
'
' Apply Freeze at cell B7 to all sheets

Application.ScreenUpdating = False
Application.EnableEvents = False

Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Range("A1") = "L2" Then
            ActiveWindow.SmallScroll Down:=-1000
            Range("B7:B7").Select
            With Selection
                ActiveWindow.FreezePanes = True
            End With
        End If
    Next ws

Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

Any direction or assistance is always appreciated.
Many thanks in advance.
Adam
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this,

VBA Code:
Sub FreezeL6()
    '
    ' Apply Freeze at cell B7 to all sheets
    
    Dim ws As Worksheet
    Dim lWS_Visible_State As Long
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
        
    For Each ws In Worksheets
        If ws.Range("A1") = "L2" Then
            lWS_Visible_State = ws.Visible
            ws.Visible = xlSheetVisible
            Application.Goto ws.Range("B7")
            ActiveWindow.FreezePanes = True
            ws.Visible = lWS_Visible_State
        End If
    Next ws
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Try this,

VBA Code:
Sub FreezeL6()
    '
    ' Apply Freeze at cell B7 to all sheets
   
    Dim ws As Worksheet
    Dim lWS_Visible_State As Long
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
       
    For Each ws In Worksheets
        If ws.Range("A1") = "L2" Then
            lWS_Visible_State = ws.Visible
            ws.Visible = xlSheetVisible
            Application.Goto ws.Range("B7")
            ActiveWindow.FreezePanes = True
            ws.Visible = lWS_Visible_State
        End If
    Next ws
   
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
Wow! How do you do that? Thank you AlphaFrog, it worked perfectly, I as so grateful, I would never have come up with that in a million years. I am now going to study your code to understand this as it will help me other routines I have to perform of a similar nature, you have taught me so much, plus saved me hours, thank you so much.
Regards
Adam
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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