User Form With Multiple Passwords

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
123
HI Team,

I have using the below code which is working fine as I have only one password "123" (Macro 1) while login into excel using user form.

However if I use multiple password with the help of OR then the macro doesn't work (Macro 2) could you please help to resolve this issue.

Macro 1 ) Working Fine with One password

Private Sub CommandButton1_Click()
If UserForm1.TextBox2.Value = "123" Then
Worksheets("Sheet1").Cells(1, 1).Value = TextBox2.Text
Unload Me
MsgBox ("Welcome !!!")
Else
Unload Me
MsgBox ("Invalid password")
UserForm1.Show
End If
End Sub

Macro 2 ) Not working with multiple passwords

Private Sub CommandButton1_Click()
If UserForm1.TextBox2.Value = "1111" Or "2222" Or "3333" Or "4444" Or "5555" Or "6666" Or "7777" Or "8888" Or "9999" Then
Worksheets("Sheet1").Cells(1, 1).Value = TextBox2.Text
Unload Me
MsgBox ("Welcome !!!")
Else
Unload Me
MsgBox ("Invalid password")
UserForm1.Show
End If
End Sub
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,477
Office Version
  1. 2019
Platform
  1. Windows
Hi,
You have to test each value against the textbox value

e.g.

VBA Code:
If UserForm1.TextBox2.Value = "1111" Or UserForm1.TextBox2.Value = "2222" Or UserForm1.TextBox2.Value = "3333" Then

But this creates lengthy code - you can as one approach, put all the values in an array & test Textbox value against this

VBA Code:
Private Sub CommandButton1_Click()
    Dim m           As Variant
    m = Application.Match(Me.TextBox2.Value, Array("1111", "2222", "3333", "4444", "5555", _
                                                    "6666", "7777", "8888", "9999"), 0)
    If Not IsError(m) Then
        Worksheets("Sheet1").Cells(1, 1).Value = Me.TextBox2.Text
        Unload Me
        
        MsgBox ("Welcome !!!")
        
    Else
        
        MsgBox ("Invalid password")
        Me.TextBox2.SetFocus
    End If
End Sub

Note I have replaced the Userform name with Me keyword where "Me" is your userform.

Dave
 

Watch MrExcel Video

Forum statistics

Threads
1,127,107
Messages
5,622,782
Members
415,927
Latest member
vedasinternational

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