Adding data to a tabel fails secons time

santnok

Board Regular
Joined
Jan 10, 2014
Messages
97
Hi

I have a userform that have a code that adds data to a tabel. I have a strange problem.
When I click the button the first time it works great. It adds the new row in the tabel but when I try to add the second product I get this error:

Run-time error '2147417848(80010108)':
Method 'Add' of object 'ListRows' failed

I have checked the line that fails and it is this line in my code: Set newrow = tbl.ListRows.Add


Here is my button code

Code:
Private Sub cmdAddProduct_Click()

Application.ScreenUpdating = False

If cmdAddProduct.Caption = "New Product" Then

txtUniqID.Value = ""
txtProductname.Value = ""
txtProductnumber.Value = ""
txtProductID.Value = ""
txtVendor.Value = ""
txtCostSafe4.Value = ""

cmdAddProduct.Caption = "Add"


Else

Dim ws As Worksheet
Set ws = Sheets("PARAM")
Dim tbl As ListObject
Set tbl = ws.ListObjects("produkt_liste")
Dim newrow As ListRow

'Henter utregnings tall
Dim Listepris As Variant
Listepris = ws.Range("T7")
Dim Partnerpris As Variant
Partnerpris = ws.Range("T8")
Dim Pris100 As Variant
Pris100 = ws.Range("T9")
Dim Pris500 As Variant
Pris500 = ws.Range("T10")
Dim Pris1000 As Variant
Pris1000 = ws.Range("T11")
Dim Pris5000 As Variant
Pris5000 = ws.Range("T12")
Dim Pris10000 As Variant
Pris10000 = ws.Range("T13")
Dim Pris25000 As Variant
Pris25000 = ws.Range("T14")
Dim Pris50000 As Variant
Pris50000 = ws.Range("T15")
Dim Pris100000 As Variant
Pris100000 = ws.Range("T16")

Set newrow = tbl.ListRows.Add

With newrow
    .Range(1) = tbl.ListRows.Count
    .Range(2) = txtProductnumber.Value
    .Range(3) = txtProductname.Value
    .Range(4) = txtProductID.Value
    .Range(5) = txtVendor.Value
    .Range(6) = txtCostSafe4.Value
    .Range(7) = .Range(6) * Listepris
    .Range(8) = .Range(6) * Partnerpris
    .Range(9) = .Range(6) * Pris100
    .Range(10) = .Range(6) * Pris500
    .Range(11) = .Range(6) * Pris1000
    .Range(12) = .Range(6) * Pris5000
    .Range(13) = .Range(6) * Pris10000
    .Range(14) = .Range(6) * Pris25000
    .Range(15) = .Range(6) * Pris50000
    .Range(16) = .Range(6) * Pris10000
    '.Range(17) = 5
End With


Dim LastRow As Long
  LastRow = Sheets("PARAM").Cells(Rows.Count, 1).End(xlUp).Row
  currentrow = LastRow
        txtUniqID = Cells(currentrow, 1)
        txtProductnumber = Cells(currentrow, 2)
        txtProductname = Cells(currentrow, 3)
        txtProductID = Cells(currentrow, 4)
        txtVendor = Cells(currentrow, 5)
        txtCostSafe4 = Cells(currentrow, 6)


ListBoxProdukter.RowSource = "produkt_liste"
cmdAddProduct.Caption = "New Product"

End If
Application.ScreenUpdating = True


End Sub

Hope some one can help me out here, thanks :-)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Can't see why that line would cause an error unless the last row of the table, prior to executing that line, is the last row of the worksheet the table is on. Then the code would be attempting to add a row that is "off the sheet".
 
Upvote 0
Hi JoeMo
Thanks for the answer, but I have no clue what I going to try. Can I PM you where you can download the workbook so you can try?
 
Upvote 0
I don't download from the web. If you PM me I will give you an email address and you can email the file to me.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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