How do I stop a Input Box Loop for a Password when user clicks cancel?

jonathanwang003

Board Regular
Joined
May 9, 2014
Messages
123
Hi guys,

So my workbook is protected. I need a macro which prompts a InputBox saying "Please Enter Password."
If the password is incorrect, it says, "Incorrect password. Please try again." and loops to the first input box.

However... if they hit cancel, I want it to shut off the document. Here's what I have that works for the loop. But then if you get the password wrong... you're screwed.

Sub UnlockWorkbook()
Const PW = "rabbit"
Dim password1 As String

Do
password1 = InputBox("Please Enter Password.")

If password1 <> PW Then
MsgBox "Incorrect password. Please try again."

Else
ActiveWorkbook.Unprotect Password:=password1
MsgBox "Password correct."
End If
Loop While password1 <> PW

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
jonathan,

Why not just remove the Do While loop ?

Hope that helps.
 
Upvote 0
jonathan,

Why not just remove the Do While loop ?

Hope that helps.

Hi Snakehips,

Thanks for responding so quickly. That's a good question. I want the inputbox which asks for password to continue to pop up if the password is wrong. If they press cancel, then I want it to close the document.

When I remove the loop, it doesn't ask for password after the first time. The reason I'm doing this is because I am integrating this into another macro which automatically opens the file and unlocks the workbook. I just can't have an unauthorized user access the workbook.

Is there any way to set the document to close if they press cancel? I'm stuck on this one.

Thanks!
 
Upvote 0
Perhaps....

Code:
Sub UnlockWorkbook()
Const PW = "rabbit"
Dim password1 As String


Do
password1 = InputBox("Please Enter Password.")


If password1 = "" Then  ' "" = no pasword entered or Cancel clicked
'message optional
If MsgBox("Do you give up guessing?", vbYesNo, "Had enough yet?") = vbYes Then
'close workbook
ThisWorkbook.Close SaveChanges:=False
Exit Do
End If
End If
If password1 <> PW Then
MsgBox "Incorrect password. Please try again."


Else
ActiveWorkbook.Unprotect Password:=password1
MsgBox "Password correct."
End If
Loop While password1 <> PW


End Sub
 
Upvote 0
This may help you...

Here is a generalized structure you can use for the InputBox that will allow you to react to the user clicking the Cancel button or clicking Enter without filling in anything as well as when the user enters actual data...
Code:
Dim Answer As String
 '....
 '....
 Answer = InputBox("Tell me something")
 If StrPtr(Answer) = 0 Then
   MsgBox "The user clicked Cancel, so we will exit the subroutine now."
   Exit Sub
 ElseIf Len(Answer) = 0 Then
   MsgBox "The user clicked OK without entering anything in the InputBox!"
 Else
   MsgBox "The user entered the following..." & vbLf & vbLf & Answer
 End If
 
Upvote 0
Thank you both for all your great ideas. They all have solved my issues with this task and I appreciate all your expertise.

:)
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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