Open all Worksheets that begin with "C_"

MarkCBB

Active Member
Joined
Apr 12, 2010
Messages
497
HI there,

I am using the following code to open worksheets, but every time I add a new chart ("C_" stands for Chart), i need to update the VBA.

Is there a way to open all worksheets that Begin with "C_" instead of the way that I am doing it?
Code:
Sub OPEN_C()

    Sheets("C_Weekly").Visible = True
    Sheets("C_CENTRAL").Visible = True
    Sheets("C_Best vs. Last").Visible = True
    Sheets("C_EASTERN CAPE").Visible = True
    Sheets("C_GAUTENG NORTH").Visible = True
    Sheets("C_GAUTENG SOUTH").Visible = True
    Sheets("C_KWAZULU NATAL").Visible = True
    Sheets("C_LIMPOPO").Visible = True
    Sheets("C_MPUMALANGA").Visible = True
    Sheets("C_Western Cape").Visible = True
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] OPEN_C()

    [color=darkblue]Dim[/color] wks [color=darkblue]As[/color] Worksheet
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] wks [color=darkblue]In[/color] ActiveWorkbook.Worksheets
        [color=darkblue]If[/color] Left(wks.Name, 1) = "C" [color=darkblue]Then[/color]
            wks.Visible = [color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] wks
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0
I cahnge the code to this, and it does work, is there a reason I am over looking?

Dim wks As Worksheet

For Each wks In ActiveWorkbook.Worksheets
If Left(wks.Name, 2) = "C_" Then
wks.Visible = False
End If
Next wks
 
Upvote 0
Sorry, somehow I missed the fact that you wanted to check for "C_", not just "C". And, I'm assuming you actually want...

Code:
wks.Visible = True
 
Upvote 0
If these are chart sheets you might need to use Sheets instead of Worksheets.

Try this code to see if any of the sheets are actually charts.
Code:
Dim ws
 
For Each ws In ThisWorkbook.Sheets
    Debug.Print ws.Name & "-" & TypeName(ws)
Next ws
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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