I am trying to provide a minor form of access protection to multiple sheets in an Excel workbook. I understand this isn't easy to achieve and there will still be issues with protection.
I have working code below on how I want to achieve this. However, it will only work on one sheet in the workbook. Is there a way to add multiple sheets to this code.
Note: I don't wan't to create multiple versions of the same workbook. I just want a simple password to access the sheet. I understand this doesn't provide a foolproof method or restricting access
Private Sub Workbook_Open()
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheetName As String
MySheetName = "Sheet1" 'The first sheet which I want to hide.
MySheetName = "Sheet2" 'The second sheet which I want to hide.
If Application.ActiveSheet.Name = MySheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
response = Application.InputBox("Password", "Enter Password", "", Type:=2)
If response = "1234" Then 'Unhide Password.
Application.Sheets(MySheetName).Visible = True
Application.Sheets(MySheetName).Select
End If
End If
Application.Sheets(MySheetName).Visible = True
Application.EnableEvents = True
End Sub
This code will only work on a single worksheet. Can it be adapted to provide protection on multiple sheets?
MySheetName = "Sheet1"then immediately overwrite it withMySheetName = "Sheet2". That's why it only works on sheet2. Also, you have a parameter to the event,Shthat refers to the sheet just activated, use that rather thanActiveSheet. Thirdly, when you hide the active sheet another sheets becomes active. That might be the other sheet you want to protect.