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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How many other sheets are there in the workbook?
How are they named?
We can simply tell it to loop through all sheets and hide all the ones meeting certain name criteria.
 
Upvote 0
How about
VBA Code:
Sub HIDE_ALL()
   Dim i As Long
   With ThisWorkbook
      .Unprotect Password:="1111"
      For i = 1 To 7
         .Worksheets("WB_" & i).Visible = xlSheetHidden
      Next i
      .Protect Password:="1111"
   End With
End Sub
 
Upvote 0
The amount of worksheets vary.

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.

Then, there will be up to 8 other worksheets that should not be hidden.

These worksheets are named this way: today's date | three-letter-symbol

For Example: 13.8.21 | AL1
 
Upvote 0
How about
VBA Code:
Sub HIDE_ALL()
   Dim i As Long
   With ThisWorkbook
      .Unprotect Password:="1111"
      For i = 1 To 7
         .Worksheets("WB_" & i).Visible = xlSheetHidden
      Next i
      .Protect Password:="1111"
   End With
End Sub
Looking good Fluff.

How would it work if instead WB_1 or WB_2 name of sheet there will be some random text?

For Example WB_Machines or WB_Service.

Thank you.
 
Upvote 0
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_*" Then Ws.Visible = xlSheetHidden
      Next Ws
      .Protect Password:="1111"
   End With
End Sub
 
Upvote 0
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_*" Then Ws.Visible = xlSheetHidden
      Next Ws
      .Protect Password:="1111"
   End With
End Sub
Awesome.

One small wish.

Would it be possible to add maybe an OR operator because there is one worksheet called like no other "CALC".
 
Upvote 0
If you want you can simply type in the exact sheet names in the VBA code

VBA Code:
Sub HIDE_ALL()
    Dim ws As Worksheet
    Set WSArray = Worksheets(Array("WB_1", "WB_2", "WB_3", "WB_4", "WB_5", "WB_6", "WB_7"))
        With ThisWorkbook
        .Unprotect Password:="1111"

    For Each ws In WSArray
    ws.Visible = xlSheetHidden
    Next ws
        .Protect Password:="1111"
End With
End Sub
 
Upvote 0
If you want you can simply type in the exact sheet names in the VBA code

VBA Code:
Sub HIDE_ALL()
    Dim ws As Worksheet
    Set WSArray = Worksheets(Array("WB_1", "WB_2", "WB_3", "WB_4", "WB_5", "WB_6", "WB_7"))
        With ThisWorkbook
        .Unprotect Password:="1111"

    For Each ws In WSArray
    ws.Visible = xlSheetHidden
    Next ws
        .Protect Password:="1111"
End With
End Sub
Looking good. Thank you hrayani.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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