VBA excel: Parsing a parameter freeze pane for each worksheet

Hello Experts,

I use the following procedure to freeze panes for each worksheets based on a worksheet name that contains a specific character:

Sub Freeze(SheetNameString As String, sSheetName As Worksheet)

    Dim ws As Worksheet
    
     Application.DisplayAlerts = False
    
    For Each ws In ActiveWorkbook.Worksheets
       If ws.Name Like " & * & sSheetNameString & * & " Then
            ws.Activate
            ActiveWindow.FreezePanes = False
            ws.Rows("4:4").Select
            ActiveWindow.FreezePanes = True
        Else:
         ws.Activate
            ActiveWindow.FreezePanes = False
            ws.Rows("2:2").Select
            ActiveWindow.FreezePanes = True
            
        End If
    Next ws
    
    Application.DisplayAlerts = True
    
    sSheetName.Select
    
End Sub

Sub RunFreeze()
Freeze "Gap", Sheets("Parameter-SAP-CCWT")
End Sub

Open in new window


In my example a Freeze panes in  Rows("4:4") if the ws.name contains the string "Gap" else I freeze Rows("1:1").

However I don't know how should I define my   sSheetNameString,

thank you again for your help.
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasMcA2Commented:
Does the posted code work? The sub gets SheetNameString as an input parm, but you are using sSheetNameString (with an extra leading "s") within the code.

If you remove that extra leading "s", your code should work.

But your post implies that it already works.

Is sSheetNameString defined as a global variable somewhere else in your code?
0
Martin LissOlder than dirtCommented:
Try this.
Sub Freeze(SheetNameString As String, sSheetName As String)

    Dim ws As Worksheet
    
    Application.DisplayAlerts = False
    
    For Each ws In ActiveWorkbook.Worksheets
'       If ws.Name Like " & * & SheetNameString & * & " Then
       If ws.Name Like "*" & SheetNameString & "*" Then
            ws.Activate
            ActiveWindow.FreezePanes = False
            ws.Rows("4:4").Select
            ActiveWindow.FreezePanes = True
        Else
            ws.Activate
            ActiveWindow.FreezePanes = False
            ws.Rows("2:2").Select
            ActiveWindow.FreezePanes = True
            
        End If
    Next ws
    
    Application.DisplayAlerts = True
    Worksheets(sSheetName).Select
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
@ Martin: It works thank you very much.

Just a few remake:

1 if I want to integrate Rows("4:4") and Rows("2:2") for the freeze selection as parameters, such as rws1 and rws2 how should I declare them as Range beside the Procedure?

And how should I call them "4:4", "2:2" or like this "Rows("4:4") Rows("2:2")?

Thank you very much for your very much for help
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Martin LissOlder than dirtCommented:
I'm not exactly sure what you mean but Rows looks for a number so Rows(2) or Rows(4) will work.
0
LD16Author Commented:
Ok, I was able to get a my solution:

Sub Freeze2(SheetNameString As String, SheetName As String, Rws1 As String, Rws2 As String)

    Dim ws As Worksheet
    
    Application.DisplayAlerts = False
    
    For Each ws In ActiveWorkbook.Worksheets
'       If ws.Name Like " & * & SheetNameString & * & " Then
       If ws.Name Like "*" & SheetNameString & "*" Then
            ws.Activate
            ActiveWindow.FreezePanes = False
            ws.Rows(Rws1).Select
            ActiveWindow.FreezePanes = True
        Else
            ws.Activate
            ActiveWindow.FreezePanes = False
            ws.Rows(Rws2).Select
            ActiveWindow.FreezePanes = True
            
        End If
    Next ws
    
    Application.DisplayAlerts = True
    Worksheets(SheetName).Select
End Sub

Sub RunFreeze()
Freeze2 "Gap", ActiveSheet.Name, "4:4", "2:2"
End Sub

Open in new window

0
LD16Author Commented:
Thank you very much for your help!
0
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.