Excel novice here again - old dog new tricks. I've been working on some data entry userforms in my workbook, and an encountering a runtime error whenever I try to add new blank rows to an existing named table. The runtime error code is -2147417848 (80010108) "Method 'Add' of object 'ListRows' failed. The table is not protected, the cells are not protected, the table only has 16 existing rows. I've tried reconstructing the code from scratch and splitting the code up to separate subroutines to first add the empty rows, then add data to the rows. Can anyone decipher why this happens? I really need to solve this so I can complete the rest of my work. Here's the code:
Private Sub cmdSaveTB_Click()
Dim ws As Worksheet
Dim tbl As ListObject
Dim lastRow As Long
Dim i As Long
Dim username As String
Dim currentDate As Date
Dim valuesArray As Variant
' Set references to the worksheet and table
Set ws = ThisWorkbook.Sheets("TB_Database")
Set tbl = ws.ListObjects("tbl_TBTabulation")
ValidateTBEntries
' Add 8 empty rows to the table
For i = 1 To 8
tbl.ListRows.Add
Next i
' Find the last row in the table
lastRow = tbl.ListRows.Count
' Copy Wall ID value to "Wall ID" column
If Trim(Me.cmbWallID.Value) <> "" Then
For i = 0 To 7
ws.Cells(lastRow - i, tbl.ListColumns("Wall ID").Index).Value = Me.cmbWallID.Value
Next i
End If
' Copy Wall Description value to "Wall Description" column
If Trim(Me.lstWallDescription.Value) <> "" Then
For i = 0 To 7
ws.Cells(lastRow - i, tbl.ListColumns("Wall Description").Index).Value = Me.lstWallDescription.Value
Next i
End If
' Copy values from textboxes to "Joint Length" column
valuesArray = Array(Me.txtTBBase.Value, Me.txtTBParapet.Value, Me.txtTBRoofWall.Value, _
Me.txtTBSlabNBR.Value, Me.txtTBSlabBR.Value, Me.txtTBCorner.Value, _
Me.txtTBOpenPerim.Value, Me.txtTBIntWall.Value)
For i = LBound(valuesArray) To UBound(valuesArray)
ws.Cells(lastRow - i, tbl.ListColumns("Joint Length").Index).Value = valuesArray(i)
Next i
' Get username and current date
username = Environ("USERNAME")
currentDate = Date
' Copy username to "Entered By" column
For i = 0 To 7
ws.Cells(lastRow - i, tbl.ListColumns("Entered By").Index).Value = username
Next i
' Copy date to "Date Entered" column
For i = 0 To 7
ws.Cells(lastRow - i, tbl.ListColumns("Date Entered").Index).Value = currentDate
Next i
' Clear the combobox and listbox values after copying
Me.cmbWallID.Value = ""
Me.lstWallDescription.Clear
' Clear values in text boxes
Me.txtTBBase.Value = ""
Me.txtTBParapet.Value = ""
Me.txtTBRoofWall.Value = ""
Me.txtTBSlabNBR.Value = ""
Me.txtTBSlabBR.Value = ""
Me.txtTBCorner.Value = ""
Me.txtTBOpenPerim.Value = ""
Me.txtTBIntWall.Value = ""
Me.TBImage.Picture = LoadPicture("")
MsgBox "Data saved successfully.", vbInformation
End Sub
Administrator's note: Please take the time to review our rules. There aren't many, and they are all important. Our guidelines recommend code tags. I have added them for you this time because you are a new member. --6StringJazzer
Bookmarks