I have created a UserForm that allows people to log-in to my worksheet -using stored information in A:A(username) and B:B (password).
On the same sheet, I have a cell in which I would like the UserForm to paste the "Username" entry of the form into Cell J2 (this will be used to check the current user logged in). It's really frustrating as I have found the route cause, and tried many different scenarios, to no avail.
My full code is below, the highlighted range (red) seems to be the thing causing all of these problems. I can run the code once (on occasion twice) before it takes me to the VBE and highlights the "Private Sub LogInButton_Click() UserLogIn.Show part, with a message saying the code had been interrupted.
as soon as you remove it from the code it works perfectly. Is there a way around this?
UserForm Code
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub ClearButton_Click()
Call UserForm_Initialize
End Sub
Private Sub OKButton_Click()
Sheets("LogIn").Activate
On Error GoTo user_not_Found
If TextPassword.Value = Range("B" & Application.WorksheetFunction.Match(TextUsername.Value, Range("A:A"), 0)) Then
MsgBox "Welcome back, " & TextUsername.Value & ".", vbOKOnly, "Success"
Unload Me
Range("J2").Value = TextUsername.Value
Else
GoTo user_not_Found
End If
Exit Sub
user_not_Found:
MsgBox "Incorrect details given", vbExclamation, "Incorrect"
End Sub
Private Sub UserForm_Initialize()
'Empty Username Text
TextUsername.Value = ""
'Empty Password Text
TextPassword.Value = ""
'
'Set Focus on NameTextBox
TextUsername.SetFocus
End Sub
Sheet Code
Private Sub LogInButton_Click()
UserLogIn.Show
End Sub
Private Sub RegisterButton_Click()
UserRegister.Show
End Sub
Bookmarks