Macro asking for a password twice when unprotecting sheet

G

Guest

I attached the following code attached to a Command Button. The purpose is
to ask the user for the unprotect password and if the password is correct, to
unhide cells and unprotect the sheet. My only problem is that this asks the
user for the password twice. I felt it was necessary to add the If, Then,
Else component because otherwise the unprotect command would allow the user
to "debug" if the password was entered incorrectly which would allow them to
see the protect code with the password built in.

I understand why it is asking for the password twice, I just don't know of a
way around it. Also, I am a novice so any help is greatly appreciated.
Using VBA in Excel 2003.

Thanks in advance

'Private Sub CommandButton1_Click()

'must lock VBA project so you can't see the password in it
Dim MyStr1 As String, MyStr2 As String
MyStr2 = ("show$") 'This is the password and it is CASE sensitive
MyStr1 = InputBox("Password Required")
If MyStr1 = MyStr2 Then
Rows("33:40").Select
Selection.EntireRow.Hidden = False
ActiveSheet.Unprotect
Else
MsgBox ("Access Denied")
End If
End Sub
 
D

Dave Peterson

If you pass the password to this line:
ActiveSheet.Unprotect
It may work:

ActiveSheet.Unprotect password:=mystr1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top