Hide multiple sheets, simplify the code [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi,

would it be possible to simplify & speed up this code?

Thank you.

VBA Code:
Sub HIDE_ALL()

With ThisWorkbook
.Unprotect password:="1111"

If Worksheets("WB_1").Visible = True Then
Worksheets("WB_1").Visible = False
End If

If Worksheets("WB_2").Visible = True Then
Worksheets("WB_2").Visible = False
End If

If Worksheets("WB_3").Visible = True Then
Worksheets("WB_3").Visible = False
End If

If Worksheets("WB_4").Visible = True Then
Worksheets("WB_4").Visible = False
End If

If Worksheets("WB_5").Visible = True Then
Worksheets("WB_5").Visible = False
End If

If Worksheets("WB_6").Visible = True Then
Worksheets("WB_6").Visible = False
End If

If Worksheets("WB_7").Visible = True Then
Worksheets("WB_7").Visible = False
End If

.protect password:="1111"
End With

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Ok, how about
VBA Code:
Sub HIDE_ALL()
   Dim Ws As Worksheet
   With ThisWorkbook
      .Unprotect Password:="1111"
      For Each Ws In .Worksheets
         If Ws.Name Like "WB_*" Or Ws.Name = "Calc" Then Ws.Visible = xlSheetHidden
      Next Ws
      .Protect Password:="1111"
   End With
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub HIDE_ALL()
   Dim Ws As Worksheet
   With ThisWorkbook
      .Unprotect Password:="1111"
      For Each Ws In .Worksheets
         If Ws.Name Like "WB_*" Or Ws.Name = "Calc" Then Ws.Visible = xlSheetHidden
      Next Ws
      .Protect Password:="1111"
   End With
End Sub
Works perfectly.

Thank you Fluff.
 
Upvote 0
There will always be at least 8 worksheets starting from WB_1 up to WB_7 AND WB_0 which should not be hidden at all.
Just a bit of addition in the master code by @Fluff so that it does not hide sheet "WB_0"

VBA Code:
[/B]
Sub HIDE_ALL()
   Dim Ws As Worksheet
   With ThisWorkbook
      .Unprotect Password:="1111"
      For Each Ws In .Worksheets
         If Ws.Name <> "WB_0" And Ws.Name Like "WB_*" Or Ws.Name = "Calc" Then Ws.Visible = xlSheetHidden
      Next Ws
      .Protect Password:="1111"
   End With
End Sub
[B]
 
Upvote 0
Just a bit of addition in the master code by @Fluff so that it does not hide sheet "WB_0"

VBA Code:
[/B]
Sub HIDE_ALL()
   Dim Ws As Worksheet
   With ThisWorkbook
      .Unprotect Password:="1111"
      For Each Ws In .Worksheets
         If Ws.Name <> "WB_0" And Ws.Name Like "WB_*" Or Ws.Name = "Calc" Then Ws.Visible = xlSheetHidden
      Next Ws
      .Protect Password:="1111"
   End With
End Sub
[B]
Thank you Hrayani.

Works perfectly.

I appreciate it.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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