Password protecting a sheet

jamienwood

Board Regular
Joined
Apr 14, 2002
Messages
133
I have come up with the following code to protect a shhet from being opened unless you know the password. It all works, however the sheet is shown in the background while the password is entered, the question is does anyone know how to stop the sheet being shown in the background. P.S. the code goes on the page code, if anyone wants it feel free to copy it!!

Private Sub Worksheet_Activate()
OK = InputBox("Please Enter Your passwor", "Password")
If OK = 864 Then Sheets("Sheet3").Select
If OK <> 864 Then Sheets("Sheet1").Select
End Sub

the 864 is the password!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
P.S. If anyone knows of a better way of doing this please let me know, also if anyone thinks this is anygood please let me know and thanx to Ivan F Moala and brettvba for the help earlier this is a moddified version of what we tried to do!!
 
Upvote 0
Try this:

Private pwok As Boolean
Private Sub Worksheet_Activate()
If pwok = True Then Exit Sub
Sheets("Sheet1").Visible = False
ok = InputBox("Please Enter Your password", "Password")
If ok = "" Then
Sheets("Sheet1").Visible = True
pwok = True
Sheets("Sheet3").Select
pwok = False
Exit Sub
End If
If ok <> 864 Then
pwok = True
Sheets("Sheet1").Visible = True
pwok = False
Sheets("Sheet3").Select
Exit Sub
End If
If ok = 864 Then
pwok = True
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Activate
pwok = False
End If
End Sub


This hides the sheet (btw I managed to make Sheet1 the hidden one!) until the password is entered. The pwok flag is needed since making the sheet visible calls this routine again! I have also provided for the user hitting cancel.

Hope this helps
 
Upvote 0
Thats excellent Thanks very much. There was a slight problem with the code there was allitle too much of it but that was easily taken care of thanks alot!!

Private pwok As Boolean
Private Sub Worksheet_Activate()
If pwok = True Then Exit Sub
Sheets("Sheet1").Visible = False
ok = InputBox("Please Enter Your password", "Password")
If ok = "" Then
Sheets("Sheet1").Visible = True
pwok = True
Sheets("Sheet3").Select
pwok = False
Exit Sub
End If
If ok <> 864 Then
pwok = True
Sheets("Sheet1").Visible = True
pwok = False
Sheets("Sheet3").Select
Exit Sub
End If
If ok = 864 Then
pwok = True
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Activate
pwok = False
End If
End Sub



I changed it too:

Private pwok As Boolean
Private Sub Worksheet_Activate()
If pwok = True Then Exit Sub
Sheets("Sheet1").Visible = False
ok = InputBox("Please Enter Your password", "Password")
If ok <> 864 Then
pwok = True
Sheets("Sheet1").Visible = True
pwok = False
Sheets("Sheet3").Select
Exit Sub
End If
If ok = 864 Then
pwok = True
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Activate
pwok = False
End If
End Sub

i think thats right it seems to work anyways, Tanx again!!
 
Upvote 0
I tried using the same code and that works great. Is there anyway to hide the vba code behind the scenes so someone couldn't go in and use the VBA editor to find the password?
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,196
Members
448,874
Latest member
Lancelots

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