vba out of memory error

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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Eh, what is the code meant to do and where are you getting the error you indicate in the thread title?
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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