Hide all worksheets (array) if unhidden

sdoppke

Well-known Member
Joined
Jun 10, 2010
Messages
647
Hi All, i have this code, which seems like it should work, but its not... :( and its getting the best of me. I am trying to hide all worksheets except for a few in an array, if they are not allready hidden. here is what i have

Code:
Sub hidebut()
Dim ws As Worksheet, wb As Workbook
For Each ws In wb.Sheets
    If ws.Name <> Sheets(Array("Dashboard", "MyStoreInfo")) Then
        ws.Visible = False
    Else
        ws.Visible = True
Next
End Sub

Anyone know where im going wrong?

sd
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I'm not sure why you're putting the effort into an array of 2;

Code:
Sub hidebut()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
 
For Each ws In wb.Sheets
    If (ws.Name <> "Dashboard") And _
    (ws.Name <> "MyStoreInfo") Then
        ws.Visible = False
    Else
        ws.Visible = True
    End If
Next
End Sub
 
Upvote 0
I'm not sure why you're putting the effort into an array of 2;

Code:
Sub hidebut()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
 
For Each ws In wb.Sheets
    If (ws.Name <> "Dashboard") And _
    (ws.Name <> "MyStoreInfo") Then
        ws.Visible = False
    Else
        ws.Visible = True
    End If
Next
End Sub

Thank you that worked great, 2 was a bad example, sometime there are 3. Could you tell me what to modify to make it leave just 1 sheet?

Thanks again for your help!

sd
 
Upvote 0
i played with it and think this is it?

Code:
Sub Store_Info()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
 For Each ws In wb.Sheets
    If (ws.Name <> "MyStoreInfo") Then
        ws.Visible = False
    Else
        ws.Visible = True
    End If
Next
Sheets("MyStoreInfo").Select
Range("C2").Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0
i played with it and think this is it?

Code:
Sub Store_Info()
Dim ws As Worksheet, wb As Workbook
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
 For Each ws In wb.Sheets
    If (ws.Name <> "MyStoreInfo") Then
        ws.Visible = False
    Else
        ws.Visible = True
    End If
Next
Sheets("MyStoreInfo").Select
Range("C2").Select
Application.ScreenUpdating = True
End Sub


No Actually that doesnt work for just one sheet ("MyStoreInfo")

Any ideas?

sd
 
Upvote 0
ok looks like the problem im having is when i run the macro and the:
Code:
For Each ws In wb.Sheets
    If (ws.Name <> "MyStoreInfo") Then
        ws.Visible = False
is allready hidden, it doesnt work...
 
Upvote 0
You're getting there; I don't see any issue with the code in post #3.
You can still set the visible property to false, even if it is already false.
(One may not .Select a hidden sheet)
 
Upvote 0
This hides them all (apart from a dummy sheet created for the purpose) then reveals anything in the array
Code:
Sub hideallbut()
    Dim ws, ma, ts
    ma = Array("sheet2", "sheet3")
    Application.ScreenUpdating = False
    Set ts = Sheets.Add
    For Each ws In ActiveWorkbook.Sheets
        If ws.Name <> ts.Name Then ws.Visible = False
    Next
    For Each a In ma
        Sheets(a).Visible = True
    Next
    Application.DisplayAlerts = False
    ts.Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,531
Messages
6,179,379
Members
452,907
Latest member
Roland Deschain

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