VBA code to hide and unhide sheets

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hi, I'm using this VBA code to hide and unhide sheets. But I would like it to be the sheets I wrote in the line With Sheets ("Master Data"), ie With Sheets ("Stamdata", "Tid", "Kostplan") or others. Someone who can help, all help will be appreciated. Best Regards Klaus W

VBA Code:
Sub Tilmelding_Rektangelafrundedehjørner1_Klik()
Dim a As String
    With Sheets("Stamdata")
        Do
            .Visible = xlVeryHidden
            a = InputBox("Indtast adgangskode for at åbne som Administrator", "Begrænset adgang")
            If Len(a) = 0 Then Exit Sub
            If a = "1234" Then
                .Visible = xlSheetVisible
                Exit Do
            Else
                MsgBox "Forkert adgangskode", 16, "Error"
            End If
        Loop
    End With
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How to hide Tabs??

Call hide


Sub hide()
Sheets(" Master Data").Visible = False
End Sub


Sub Show()
Sheets(" Master Data").Visible = True
End Sub
 
Upvote 0
I got error in this line .Visible = xlVeryHidden
 
Upvote 0
How about:

VBA Code:
Sub Tilmelding_Rektangelafrundedehjørner1_Klik()
'
    Dim a As String
'
    SheetNames = Array("Stamdata", "Tid", "Kostplan")
'
    For Each SheetName In SheetNames
        With Sheets(SheetName)
            Do
                .Visible = xlVeryHidden
                a = InputBox("Indtast adgangskode for at åbne som Administrator", "Begrænset adgang")
                If Len(a) = 0 Then Exit Sub
                If a = "1234" Then
                    .Visible = xlSheetVisible
                    Exit Do
                Else
                    MsgBox "Forkert adgangskode", 16, "Error"
                End If
            Loop
        End With
    Next
End Sub
 
Upvote 0
How about:

VBA Code:
Sub Tilmelding_Rektangelafrundedehjørner1_Klik()
'
    Dim a As String
'
    SheetNames = Array("Stamdata", "Tid", "Kostplan")
'
    For Each SheetName In SheetNames
        With Sheets(SheetName)
            Do
                .Visible = xlVeryHidden
                a = InputBox("Indtast adgangskode for at åbne som Administrator", "Begrænset adgang")
                If Len(a) = 0 Then Exit Sub
                If a = "1234" Then
                    .Visible = xlSheetVisible
                    Exit Do
                Else
                    MsgBox "Forkert adgangskode", 16, "Error"
                End If
            Loop
        End With
    Next
End Sub
It takes a sheets access where I have to enter the code every time also comes the sheets that have just become unhide visible again.So it is not like that. When I run the VBA code, it should come up with a box where I enter the code, it does so in the original code. If the code is correct, it must hide all the sheets in the line. With Sheets("Stamdata", "Tid", "Kostplan", "Kostplan 1", "Salg af proviant til gæster", "Samlet"), and when I run it again, the VBA code must come up with a box where i I enter the code. If the code is correct, it must hide all the sheets in the line. With Sheets("Stamdata", "Tid", "Kostplan", "Kostplan 1", "Salg af proviant til gæster", "Samlet"). KW
 
Upvote 0
It sounds/looks like you want to hide all the sheets in the list, then ask for a password, if password is correct it unhides all the sheets in the list?

VBA Code:
Sub Tilmelding_Rektangelafrundedehjørner1_Klik()
'
    Dim a As String
'
    SheetNames = Array("Stamdata", "Tid", "Kostplan")
'
    For Each SheetName In SheetNames
        Sheets(SheetName).Visible = xlVeryHidden                                            ' Hide all sheets in array list
    Next
'
    a = InputBox("Indtast adgangskode for at åbne som Administrator", "Begrænset adgang")   ' ask for password

    If Len(a) = 0 Then Exit Sub
'
    If a = "1234" Then                                                                      ' if password correct then ...
        For Each SheetName In SheetNames                                                    ' unhide all the sheets in the array list
            Sheets(SheetName).Visible = xlSheetVisible
        Next
'
        Exit Sub
    Else
        MsgBox "Forkert adgangskode", 16, "Error"                                           ' indicate error for password entered
    End If
End Sub
 
Upvote 0
Solution
It sounds/looks like you want to hide all the sheets in the list, then ask for a password, if password is correct it unhides all the sheets in the list?

VBA Code:
Sub Tilmelding_Rektangelafrundedehjørner1_Klik()
'
    Dim a As String
'
    SheetNames = Array("Stamdata", "Tid", "Kostplan")
'
    For Each SheetName In SheetNames
        Sheets(SheetName).Visible = xlVeryHidden                                            ' Hide all sheets in array list
    Next
'
    a = InputBox("Indtast adgangskode for at åbne som Administrator", "Begrænset adgang")   ' ask for password

    If Len(a) = 0 Then Exit Sub
'
    If a = "1234" Then                                                                      ' if password correct then ...
        For Each SheetName In SheetNames                                                    ' unhide all the sheets in the array list
            Sheets(SheetName).Visible = xlSheetVisible
        Next
'
        Exit Sub
    Else
        MsgBox "Forkert adgangskode", 16, "Error"                                           ' indicate error for password entered
    End If
End Sub
Hi johnnyL
Just as it should be. 1000 thanks for the help, have a nice day from Denmark Klaus W
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,827
Members
449,051
Latest member
excelquestion515

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