Microsoft Office Tutorials and References
In Depth Information
Try It
End the If structure that determined if the InputBox entry matched the password “hello”:
End If
End the Select Case structure for the users to confirm their intention of unprotecting the
End Select
Here is what the complete macro would look like:
Sub PasswordTest()
‘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.
Case vbNo
MsgBox “No problem -- this macro will end.”, vbInformation, “You clicked No.”
Exit Sub
‘Handle the case for a Yes answer by providing an InputBox
‘for the user to enter the password.
Case vbYes
‘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.
MsgBox _
“Sorry, “ & myPassword & “ is not the correct Password.”, _
vbCritical, _
‘The code to execute only if the correct password is entered.
MsgBox _
Search JabSto ::

Custom Search