Password Protect a Worksheet from Viewing

music_al

Board Regular
Joined
Nov 26, 2008
Messages
133
Hi

I want users to be able to click on a worksheet and then be prompted to enter a password before they can view it. I found this on another thread but a row of code is highlighted in red and it doesn't seem to work.

I get Compile Error Syntax Error.

Public PvSh As String
Public Pwd As String


Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Dim Num as Integer

If Pwd = "" Then
If Sh.Name = "Sheet2" Then
Num = ActiveWindow.Index
Windows(Num).Visible = False
If Application.InputBox("Enter Password", "Password") <> "airplane" Then
MsgBox "Incorrect Password", vbCritical, "Error"
Application.EnableEvents = False
Sheets(PvSh).Select
Application.EnableEvents = True
Else
Pwd = "airplane"
End If
Windows(Num).Visible = True
End If
End If
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
PvSh = Sh.Name
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I have just copy and pasted your code into my own workbook and it ran fine - all I can suggest is you try the same into a new workbook.
 
Upvote 0
It works when my sheets are just called Sheet1, Sheet2 and Sheet3.

I have several sheets. The only sheet that I do not need to be password protected is called "Data". All other sheets; "Roles", "Grades" etc need to be password protected.

I changed the second line to...

If Sh.Name <> "Data" Then
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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