Andy M
Andy M

Reputation: 187

Adding Data to a Excel Table

I am struggling to find out how to add data to a table. I have the following code:

Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("Table1")
tbl.ListRows.Add 1

This adds a new row at the top of the table but how do you then add data to the specific columns in that new table row?

Column 1 in the table = Date
Column 2 in the table = Licence 1
Column 3 in the table = Licence 2
Column 4 in the table = Licence 3

I know if I wanted to add a new row to the bottom of the table then I would use this:

Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("Table1")
Set NewRow = tbl.ListRows.Add
With NewRow
    .Range(1) = date
    .Range(2) = 378
    .Range(3) = 678
    .Range(4) = 897
End With

But cannot not get it to work for a specific row on the table

Thanks

Upvotes: 0

Views: 1305

Answers (1)

BigBen
BigBen

Reputation: 50162

Change

Set NewRow = tbl.ListRows.Add

to

Set NewRow = tbl.ListRows.Add(1)

If you don't specify the position, the row will always be added at the end. (You had it right in the first snippet.)

Upvotes: 1

Related Questions