Microsoft Office Tutorials and References
In Depth Information
Try It
Begin a Select Case structure with a Yes No Question message box to ask the users to
confirm their intention 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 macro with the Exit Sub statement:
Case vbNo
MsgBox “No problem -- this macro will end.”, vbInformation, “You clicked No.”
Exit Sub
Handle the case for a Yes answer:
Case vbYes
Provide an InputBox for the user to enter the password. Declare a String type variable, and
define it as the text that will be entered into the InputBox .
Dim myPassword As String
‘myPassword = _
InputBox(“Please enter the case-sensitive password:”, _
“A password is required to unprotect this worksheet.”)
Here is an opportunity to add a single-line If statement to end the macro if the user clicks
Cancel, or clicks OK without entering anything into the InputBox . The pair of double quotes
with nothing between them is interpreted by VBA as a zero-length string.
If myPassword = “” Then Exit Sub
Begin an If…Then structure to determine if the InputBox entry matches the password
“hello” that was used to protect the worksheet:
If myPassword <> “hello” Then
If the InputBox entry is anything other than “hello,” enter the code you would want
executed when an incorrect password is entered, which you can do with a friendly message box:
MsgBox _
“Sorry, “ & myPassword & “ is not the correct Password.”, _
vbCritical, _
Enter your Else statement and supply the code to be executed only if the correct password is
MsgBox _
“Thank you. Please click OK to unprotect the worksheet.”, _
vbInformation, _
“You entered the correct password!!”
ActiveSheet.Unprotect “hello”
Search JabSto ::

Custom Search