VBA password request before sub starts

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

Is there a way for the sub code to prompt for a password before it allows for the sub to continue?

Where if the incorrect password is provided by the user it exits the sub.

Something like this would help a lot!

Thanks,
Pinaceous
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Something like
Code:
Sub Pwrd()

    Dim Ans As String
    
    Ans = InputBox("Please enter a password")
    If Not Ans = "MyPassword" Then
        MsgBox "Oops"
        Exit Sub
    End If
    MsgBox "OK"
    
    
End Sub
 
Upvote 0
However, if you want the password masked, you'll need a Userform.
Create Userform with a textbox & command button.
In a standard module
Code:
Option Explicit
Public Cont As Boolean

Sub Pwrd()

    [COLOR=#ff0000]PassWrdFrm[/COLOR].Show
    If Not Cont Then
        MsgBox "Oops"
        Exit Sub
    End If
    
    MsgBox "OK"
    
End Sub
Changing the Userform name in red to suit.
And then in the Userform module
Code:
Private Sub CommandButton1_Click()
    
    If Not TextBox1.Value = "MyPassword" Then
        Cont = False
    Else
        Cont = True
    End If
    Unload Me
End Sub

Private Sub UserForm_Initialize()

    TextBox1.PasswordChar = "*"

End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Hey Fluff,

I didn't get a chance to test it out but what did you mean by
if you want the password masked
.

I am familiar with Userform but isn't the password automatically masked in VBA?

Thanks,
Pinaceous
 
Upvote 0
With an input box, you can see what pass word you type in, but with a userform you can mask the password.
Rather than seeing Password you'll see ********
 
Upvote 0
Oh I see now. That is much clearer for me and totally makes sence. Thanks again Fluff!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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