zakizelani
New Member
- Joined
- Mar 3, 2016
- Messages
- 25
Do anyone know how I can simplify this codes????
Code:
Private Sub CommandButton2_Click()'Declare the variables
Dim AddData As Range, Current As Range
Dim user As Variant, Code As Variant
Dim PName As Variant, AName As Variant
Dim ws As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim result As Integer
Dim TitleStr As String
Dim msg As VbMsgBoxResult
'Variables
user = Me.TextBox1.Value
Code = Me.TextBox2.Value
TitleStr = "Password check"
result = 0
Set Current = Sheet4.Range("AI2")
'Error handler
On Error GoTo errHandler:
'Destination location for login storage
Set AddData = Sheet9.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
'Check the login and passcode for the administrator
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each AName In Sheet3.Range("L2")
'If AName = Code Then 'Use this for passcode text
If AName = CLng(Code) And AName.Offset(0, -1) = user Then ' Use this for passcode numbers only
MsgBox "Welcome Admin: – " & user
'record user login
AddData.Value = user
AddData.Offset(0, 1).Value = Now
AddData.Offset(0, 2).Value = Now
'Change variable if the condition is meet
result = 1
'Unload the form
Unload Me
Exit Sub
End If
Next AName
End If
'Check user login with loop
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each PName In Sheet4.Range("AI:AI")
'If PName = Code Then 'Use this for passcode text
If PName = CLng(Code) And PName.Offset(0, -1) = user Then ' Use this for passcode numbers only
MsgBox "Welcome Back: – " & user & " " & Code
'record user login
AddData.Value = user
AddData.Offset(0, 1).Value = Now
AddData.Offset(0, 2).Value = Now
'Change variable if the condition is meet
result = 1
'Unload the form
Unload Me
Exit Sub
End If
Next PName
End If
'Check to see if an error occurred
If result = 0 Then
'Increment error variable
msg = MsgBox("Wrong password,Please Try Again Or Notify The Administrator ", vbCritical + vbOKOnly, TitleStr)
End If
Exit Sub
'Error block
errHandler:
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub