Microsoft Office Tutorials and References
In Depth Information
End the If structure that determined if the InputBox entry matched the password “hello”:
End the Select Case structure for the users to confirm their intention of unprotecting the
Here is what the complete macro would look like:
‘Ask the user if they want to unprotect the worksheet.
Select Case MsgBox( _
“Do you want to unprotect the worksheet?”, _
vbYesNo + vbQuestion, _
“Please confirm your intentions.”)
‘Handle the case for a No answer by informing the user
‘that the macro will not continue,
‘and then exit the subroutine with the Exit Sub statement.
MsgBox “No problem -- this macro will end.”, vbInformation, “You clicked No.”
‘Handle the case for a Yes answer by providing an InputBox
‘for the user to enter the password.
‘Declare a String type variable.
Dim myPassword As String
‘Define the String variable as the text that will be entered into the InputBox.
myPassword = _
InputBox(“Please enter the case-sensitive password:”, _
“A password is required to unprotect this worksheet.”)
‘A one-line If statement to end the macro if the user clicks Cancel,
‘or clicks OK without entering anything into the InputBox.
If myPassword = “” Then Exit Sub
‘If structure to determine if the InputBox entry matches the password “hello”
‘that was used to protect the worksheet.
If myPassword <> “hello” Then
‘The code line to be executed if an incorrect password is entered.
“Sorry, “ & myPassword & “ is not the correct Password.”, _
‘The code to execute only if the correct password is entered.