Hi Guys,
I've been programming for what seems ages and never ran into a similar issue.
I haven't prepared the file to attach but I just want to ask if somebody ever ran into similar issues.
I have one worksheet named Database an the variable in VBA is DBSheet
The Table (Listobject) is named tbl_Data and the variable is DBTable
The code below
Public Function add_Row_2_Table() As Long
Dim tbl As ListObject
Set tbl = Worksheets(DBSheet).ListObjects(DBTable)
If tbl.ListRows.Count = 1 Then
If tbl.DataBodyRange.Cells(1, 2) = "" Then GoTo row_OK
End If
add_Row:
tbl.ListRows.Add AlwaysInsert:=True
DoEvents
row_OK:
add_Row_2_Table = tbl.ListRows.Count
End Function
Works perfectly and adds a row to the end of the Listobject and it returns the correct row number in the table, this table holds 33 rows and after adding 34, so thta's correct
I also have a Userform and a button to add data
Private Sub CmbSave_Click()
If MsgBox("Do you want to save the data?", vbYesNo + vbInformation, "Confirmation") <> vbYes Then Exit Sub
Dim tbl As ListObject
Dim iRow As Long
Set tbl = Worksheets(DBSheet).ListObjects(DBTable)
Call add_Row_2_Table
iRow = tbl.ListRows.Count
tbl.DataBodyRange.Cells(iRow, 2) = CDate(Me.TxtDate)
tbl.DataBodyRange.Cells(iRow, 3) = CDbl(Me.TxtAmount.Value)
tbl.DataBodyRange.Cells(iRow, 4) = Me.CmbShop.Text
tbl.DataBodyRange.Cells(iRow, 5) = Me.CmbItem.Text
tbl.DataBodyRange.Cells(iRow, 6) = Val(Me.TxtPunnets)
tbl.DataBodyRange.Cells(iRow, 7) = IIf(Me.OpSales.Value = True, "Sale", "Bill")
Set tbl = Nothing
Call Reset
End Sub
The moment I confiem and the code 'add_Row_2_Table' is run it thows an error unable to address the ListObjec bla bla and Excel just closes, no chance to press debug or anything, it triggers the error immediately
Any ideas?
I've rebuilt the table, it has 7 columns and the first column a formula to place the row number in the table
the formula:
=ROW()-ROW(tbl_Worms[[#Headers],[Record Nr.]])
If I remove the formula the same happens so that's not it
Worksheet is unprotected and unlocked.
Any ideas would be welcome
Bookmarks