VBA Help on Password Protected Tab

Tennisball

New Member
Joined
Aug 2, 2010
Messages
5
I have created a macro to protect a tab/sheet with a password. I am wondering how I can modify the macros so that once the password is entered correctly the first time, the macro does not keep asking to enter a password. I want it so that it only gets prompted the first time the sheet is activated and when the password is entered incorrectly. Any ideas?

Private Sub Worksheet_Activate()
Dim Password
Password = Application.InputBox("Please enter a valid password.")
If Password <> "pword" Then
MsgBox "This is not for you to view, please do not try to access this sheet!"
Sheets("Sheet1").Select
If pword <> "pword" Then Sheets("Sheet2").Visible = False
Else
ActiveSheet.Unprotect Password
End If
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
if the password should be stored only for the current file ssession - create a variable in the begining of your code module before any Subs or Functions
when properly entered - store the password to it - so if it is NOT Nothing use it instead of prompting again.
if you want the password to live between sessions - it is another story.
btw - where do you store the password?
 
Upvote 0
I am sorry, I am a novice at VBA and don't really understand a word of what your wrote. As for the password, the password is stored in the script. the password is just "pword"
 
Upvote 0
Hi
You could hide Sheet2 with a Workbook_open event
and then use

Code:
Private Sub Worksheet_Activate()
Dim Password As String
msg = MsgBox("Do you need to open Sheet2", vbYesNo)
If msg = vbYes Then
Password = Application.InputBox("Please enter a valid password.")
Else: Exit Sub
End If
If Password <> "pword" Then
MsgBox "This is not for you to view, please do not try to access this sheet!"
exit sub
Else
Sheets("Sheet2").Visible = True
End If
End Sub
 
Upvote 0
Thanks Michael M, but your code doesn't do what i want it to do. If you don't enter a password. You can still view the sheet, and definitely don't want that. Also it doesn't hide the sheet if the password is wrong.
 
Upvote 0
Hi
I did suggest that you use a Workbook_Open event first to hide the sheet
AND THEN use the code
 
Upvote 0

Forum statistics

Threads
1,225,477
Messages
6,185,224
Members
453,283
Latest member
Shortm88

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