Hello Everyone,
I've written the following code, but I keep receiving a "Runtime error '91': Object variable or With block variable not set". I've bolded and italicized the portion of the code that is highlighted by the debugger. I'm not sure what's going on, because I believe I've ended the with statement correctly, and I've used this code for a previous user form that is working without issue. Any and all help is greatly appreciated. Thank you!
<code>
Damian37
</code>
I've written the following code, but I keep receiving a "Runtime error '91': Object variable or With block variable not set". I've bolded and italicized the portion of the code that is highlighted by the debugger. I'm not sure what's going on, because I believe I've ended the with statement correctly, and I've used this code for a previous user form that is working without issue. Any and all help is greatly appreciated. Thank you!
<code>
Rich (BB code):
Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long
Dim WS As Worksheet
Set WS = Worksheets("Data")
'find first empty row in database
lRow = WS.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
lDate = Me.txtDate.Value
'check for a date
If Trim(Me.txtDate.Value) = "" Then
Me.txtDate.SetFocus
MsgBox "Please enter a Date. Thank you."
Exit Sub
ElseIf Not IsDate(Me.txtDate.Value) Then
Me.txtDate.SetFocus
MsgBox "Please enter date in correct format. Thank you."
Exit Sub
Else
Me.txtDate.Value = Format(Me.txtDate.Value, "mm/dd/yyyy")
Exit Sub
End If
With WS
If MsgBox("Is all data correct?", vbYesNo) = vbYes Then
.Cells(lRow, 2).Value = Me.txtDate.Value
.Cells(lRow, 3).Value = Me.txtPERNR.Value
.Cells(lRow, 4).Value = Me.txtName.Value
.Cells(lRow, 5).Value = Me.txtAmt.Value
.Cells(lRow, 6).Value = Me.txtComments.Value
Else
Exit Sub
End If
End With
'clear the data
Me.txtDate.Value = ""
Me.txtPERNR.Value = ""
Me.txtName.Value = ""
Me.txtAmt.Value = ""
Me.txtComments.Value = ""
End Sub
Damian37
</code>