ws.Name Like - Isn't working?

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi

Could someone help me with these 2 lines in red;

Code:
    Dim ws As Worksheet
    
    If CheckBox1 = True Then
       [COLOR=#FF0000] If ws.Name Like "*3CT*" Then[/COLOR]
            ws.Visible = xlSheetVisible
        End If
        
    ElseIf CheckBox2 = True Then
       [COLOR=#FF0000] If ws.Name Like "*3CT*" Then[/COLOR]
            ws.Visible = xlSheetHidden
        End If
        
    ElseIf CheckBox3 = True Then
        For Each ws In Sheets
            ws.Visible = xlSheetVisible
        Next
    End If
    
    Unload Me

For the love of me I can't work it out.

Thank you in advance
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Is there somewhere in your code where you set the ws?

If not, that would seem to be the problem.
 
Upvote 0
Thanks for replying JonXL.

Forgive my lack of knowledge. But, I thought;
Code:
Dim ws As Worksheets
set the ws as the worksheets?

Thank you
 
Last edited:
Upvote 0
That declares a variable as a worksheet but does not tell what that variable is equal to.

This will make ws = to sheet1
Code:
Dim ws As Worksheet

Set ws = Sheets("Sheet1")
 
Last edited:
Upvote 0
Ah ok, thanks Scott T!

Does that mean I'll need to set every worksheet (e.g. Sheets("Sheet1","Sheet2")) as my code is checking every worksheet name for the word "3CT"?
 
Upvote 0
What happens if you modify the code like this:

Rich (BB code):
For Each ws in Sheets
     If CheckBox1 = True Then
             If ws.Name Like "*3CT*" Then
                 ws.Visible = xlSheetVisible
            End If
        
    ElseIf CheckBox2 = True Then
        If ws.Name Like "*3CT*" Then
            ws.Visible = xlSheetHidden
        End If
        
    ElseIf CheckBox3 = True Then
        ws.Visible = xlSheetVisible
    End If
Next
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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