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

jonathanwang003

Board Regular
Joined
May 9, 2014
Messages
120
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,159
Office Version
  1. 2013
Platform
  1. Windows
jonathan,

Why not just remove the Do While loop ?

Hope that helps.
 

jonathanwang003

Board Regular
Joined
May 9, 2014
Messages
120
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!
 

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,159
Office Version
  1. 2013
Platform
  1. Windows
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,975
Office Version
  1. 2016
Platform
  1. Windows
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
 

jonathanwang003

Board Regular
Joined
May 9, 2014
Messages
120
Thank you both for all your great ideas. They all have solved my issues with this task and I appreciate all your expertise.

:)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,576
Messages
5,637,180
Members
416,960
Latest member
Carbon1198

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
Top