I am using this code to password protect two pages.
For some weird reason I can hide "Sheet1" but not "Sheet2" as its always visiable.
The reason for the line Sheets(MySheet2).Visible = True
is so if someone puts in the wrong password it won't just hide it instantly.
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheets As String, Response As String
MySheet = "Sheet1"
MySheet2 ="Sheet2"
If ActiveSheet.Name = MySheet Then
ActiveSheet.Visible = False
Response = InputBox("Enter password to view sheet")
If Response = "MyPass" Then
Sheets(MySheet).Visible = True
Application.EnableEvents = False
Sheets(MySheet).Select
Application.EnableEvents = True
End If
End If
Sheets(MySheet).Visible = True
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If ActiveSheet.Name = MySheet2 Then
ActiveSheet.Visible = False
Response = InputBox("Enter password to view sheet")
If Response = "MyPass" Then
Sheets(MySheet2).Visible = True
Application.EnableEvents = False
Sheets(MySheet2).Select
Application.EnableEvents = True
End If
End If
Sheets(MySheet2).Visible = True
End Sub
I guess you're after this:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheets As String, Response As String
With ActiveSheet
Select Case .Name
Case "Sheet1", "Sheet2"
Application.EnableEvents = False
.Visible = False
Response = InputBox("Enter password to view sheet")
If Response = "MyPass" Then
.Visible = True
.Select
End If
Application.EnableEvents = True
End Select
End With
End Sub
as you should already know, this code is to be placed in ThisWorkbook
code pane
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.