Search code examples
excelvbauserform

How to make a password UserForm


I am trying to set up a UserForm to type a password into. After the password has been entered, I want four buttons to appear on the Worksheet.

The UserForm is called "UserForm2" the TextBox is called "PASSWORD" and the button to enter it is called "OK".

I would like the password to be "admin" (placeholder).

I have a CommandButton called "CommandButton1" on the main sheet that opens the UserForm with this code:

Private Sub CommandButton1_Click()

UserForm2.Show

End Sub

Now that I have the UserForm open how do I achieve my goal?

The four buttons I want to appear are standard Excel buttons with macros attached. Will the standard Excel buttons work or will I have to change to CommandButtons to hide and show them?

Also, I am struggling to get the password to be entered and then read.

I found this code online:

Sub LOGIN()

    Dim PASSWORD As String
    PASSWORD = "ADMIN"
    
    'Check if the password is correct
    If UserForm2.PASSWORD.Value <> PASSWORD Then
        'Display a message and exit the sub-routine if the password is not correct
        MsgBox "Sorry, incorrect password. Please try again.", vbOKOnly, "Incorrect Password"
        Exit Sub
    End If
    
    'The following code will only be executed if the password is correct...
    
    'Close the password input user form now that we don't need it
    Unload UserForm2  

End Sub

I am unsure how this is working and how to get it to work for me.

To summarize, I want to click a button to open the UserForm and then type in the password, then click the ok button on the UserForm and then have the four buttons appear.


Solution

  • I had 4 (ActiveX command-)buttons I set to .Visible = False prior to testing. Then another button with your code to show the UserForm.

    When you right-click your OK button in design-modus, you can choose to go into the code which will look something like (with the code you need):

    Private Sub OK_Click()
        Dim pwd As String, ws As Worksheet
        pwd = "ADMIN"
        
        'Check if the password is correct
        If Me.PASSWORD.Value <> pwd Then 'adjusted Userform2->Me according to FunThomas' comment
            'Display a message and exit the sub-routine if the password is not correct
            MsgBox "Sorry, incorrect password. Please try again.", vbOKOnly, "Incorrect Password"
            Exit Sub
        End If
        
        'The following code will only be executed if the password is correct...
        
        'Close the password input user form now that we don't need it
        
        Set ws = ThisWorkbook.Worksheets("YourSheet")
        Dim i As Long
        For i = 2 To 5 'my extra "invisible" buttons were called btn2, etc.
            ws.Shapes.Item("btn" & i).Visible = True
        Next i
        
        Me.Hide
    End Sub
    

    That should make them visible if the user types in "ADMIN", "admin" won't work here unless you don't care about capitalizing letters in a password, then just use UCase(UserForm2.PASSWORD.Value)

    Hope this is clear :)