Hide Unhide Specific Sheets

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi,

I created the below code a few months ago but just wondering how I could make it shorter?

The premise of it is that you enter the admin password and it will hide 6 admin sheets using their codenames.

This is then assigned to a button in my workbook and it will hide and unhide.

Is there a way to assign the codenames to an array variable? This will make updating easier if I need to add a new sheet or delete.

Also, to decide on opening or closing, I pass it through an IF statement in the line below. Can I use an Array(Codenames).count to switch the 6?

Code:
For Each Sh In Sheets(Array(Sheet5.Name, Sheet12.Name, Sheet2.Name, Sheet3.Name, Sheet33.Name, Sheet34.Name))

    If Sh.Visible = xlSheetVeryHidden Then I = I + 1


Next Sh


If I = 6 Then

Full code:
Code:
Private Sub HideUnAdminSheets()

Dim Password As String, PasswordEnter As String


Password = "******"


Application.ScreenUpdating = False


I = 0


For Each Sh In Sheets(Array(Sheet5.Name, Sheet12.Name, Sheet2.Name, Sheet3.Name, Sheet33.Name, Sheet34.Name))


    If Sh.Visible = xlSheetVeryHidden Then I = I + 1


Next Sh


If I = 6 Then


Retry:
PasswordEnter = InputBox("Please enter the admin password.")


    Select Case LCase(PasswordEnter)
    
        Case LCase(Password)
        
            For Each Sh In Sheets(Array(Sheet5.Name, Sheet12.Name, Sheet2.Name, Sheet3.Name, Sheet33.Name, Sheet34.Name))
            
                Sh.Visible = True
            
            Next Sh
            
            Sheet1.Activate
            
        Case Else
        
            RetryPassword = MsgBox("Would you like to retry?", vbYesNo)
            
                Select Case RetryPassword
                
                    Case vbYes
                    
                        GoTo Retry
                    
                    Case vbNo
                    
                        Exit Sub
                    
                End Select
        
    End Select
    
Else


    For Each Sh In Sheets(Array(Sheet5.Name, Sheet12.Name, Sheet2.Name, Sheet3.Name, Sheet33.Name, Sheet34.Name))
    
        Sh.Visible = xlSheetVeryHidden
    
    Next Sh
    
    Sheet1.Activate
    
End If


Application.ScreenUpdating = True


End Sub
 
I'm not too sure, I'll still need to use the above code and especially the For Each Sh in Arr..

So I'd need to store the sheets that have X in their name into the Arr no?
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Why do you need the For each sh in arr?
 
Upvote 0
Because I’m unsure of an alternate piece of code that lets me hide and unhide the sheets in one button with a password prompt
 
Upvote 0
Perhaps we can start from the beginning. What exactly is it that you want to happen?
 
Upvote 0
I have a load of sheets that contain the word “Daily” that I would like to hide and unhide with one button which has a password prompt on unhiding.

The code originally was perfect but the sheets were hard coded so if I was to remove or add another daily sheet, it wouldn’t capture it and would break.
 
Upvote 0
Try something like this (air code):

Code:
Private Sub HideUnAdminSheets()

    Dim Password As String: Password = "******"
    Application.ScreenUpdating = False
    Sheet1.Activate
    If AllHidden Then
Retry:
        If LCase(InputBox("Please enter the admin password.")) = Password Then
            Call LoopSheets(xlSheetVisible)
        Else
            If MsgBox("Would you like to retry?", vbYesNo) = vbYes Then GoTo Retry Else Exit Sub
        End If
    
    Else
        Call LoopSheets(xlSheetVeryHidden)
    End If

    Application.ScreenUpdating = True

End Sub

Private Sub LoopSheets(IsVisible)
 for each sh in sheets
if instr(1, sh.name, "Daily", vbtextcompare) <> 0 then sh.visible = IsVisible
next
End Sub
Private Function AllHidden() as boolean
AllHidden = True
for each sh in sheets
if instr(1, sh.name, "Daily", vbtextcompare) <> 0 then
    If sh.visible <> xlsheetveryhidden then
        allhidden = false
        exit for
    end if
next
End Function
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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