I'm trying to take simple data entered into two text boxes within a userform and then have this data added to a table named "AvantAct". I want the data entered into the first blank row in the table every time the userform is run. Interestingly, the first time I did this it worked flawlessly. However after exiting the workbook and then coming back to it later I seem to get: -
Run time error '91': Object variable or With block variable not set.
When I debug the following line is highlighted:
tbl.DataBodyRange(lrow2, 1). Value = Me.TxtDate.Value
I should add that my table is currently an empty (freshly inserted) table. It has 8 columns (with headers), one empty row (from being inserted) and a total row.
Any suggestions?
Private Sub SubmitButton_Click()
Dim ws As Worksheet
Dim tbl As ListObject
Dim TxtDate As Date
Dim TxtPmt As Currency
Dim col As Integer
Dim lrow As Range
Dim lrow2 As Long
Set ws = ActiveWorkbook.Worksheets("Avant")
Set tbl = ws.ListObjects.Item("AvantAct")
If tbl.ListRows.Count > 0 Then
Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
For col = 1 To lrow.Columns.Count
If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
tbl.ListRows.Add
Exit For
End If
Next col
End If
lrow2 = tbl.ListRows.Count
tbl.DataBodyRange(lrow2, 1).Value = Me.TxtDate.Value
tbl.DataBodyRange(lrow2, 3).Value = Me.TxtPmt.Value
Unload Me
End Sub