Input Box

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Good Morning,

I have the following code that's been working well as a "password box" to unlock a few sheets. However, I'd like to tweak it so that the if the password is wrong, it will display an "wrong password" message and then ask for the password again. this is all mounted on userform5 as commandbutton3.

Thanks for the help!


Code:
Private Sub CommandButton3_Click()
On Error GoTo Helper
Pass = Application.InputBox("Enter Password", "The Coding Sheets")
If Pass = CStr(Worksheets("Developer").Range("B23").Value) Then
    Sheets("Developer").Visible = True
    Sheets("Notes").Visible = True
    Sheets("Ports").Visible = True
    Sheets("Developer").Select
    Application.Visible = True
End If
Unload Me
'Error Clearing Code
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Something like this:

Code:
Private Sub CommandButton3_Click()
On Error GoTo Helper
TryAgain:
Pass = Application.InputBox("Enter Password", "The Coding Sheets")
If Pass = CStr(Worksheets("Developer").Range("B23").Value) Then
    Sheets("Developer").Visible = True
    Sheets("Notes").Visible = True
    Sheets("Ports").Visible = True
    Sheets("Developer").Select
    Application.Visible = True
Else
    MsgBox "Password incorrect. Please try again", vbCritical, "Wrong Passwrod Entered"
    GoTo TryAgain
End If
Unload Me
Exit Sub
'Error Clearing Code
Helper:
    MsgBox Err.Description
End Sub
 
Last edited:
Upvote 0
oh duh....the goto TryAgain makes perfect sense....

for some reason I was playing with other loops and it wasn't working....that was an easy go to solution. Thanks!
 
Upvote 0
You may also want to consider adding in another line
Code:
Private Sub CommandButton3_Click()
On Error GoTo Helper
TryAgain:
Pass = Application.InputBox("Enter Password", "The Coding Sheets")
[COLOR=#ff0000]If Pass = False Then Exit Sub[/COLOR]
If Pass = CStr(Worksheets("Developer").Range("B23").Value) Then
    Sheets("Developer").Visible = True
    Sheets("Notes").Visible = True
    Sheets("Ports").Visible = True
    Sheets("Developer").Select
    Application.Visible = True
Else
    MsgBox "Password incorrect. Please try again", vbCritical, "Wrong Passwrod Entered"
    GoTo TryAgain
End If
Unload Me
Exit Sub
'Error Clearing Code
Helper:
    MsgBox Err.Description
End Sub
That way if they click cancel on the input box, it exits the sub. Otherwise it will loop permanently.
 
Upvote 0
Good call....hadn't thought about that. Yeah...I was doing a quick sweep through the workbook last night before I push it out to all the machines tomorrow and realized that anyone that clicked "cancel" or exited the input box could get past all of it....minor details...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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