0

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

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

i've been searching for a good couple of hours trying to get it to bloody work, cheers fella.
can you set an existing row in the table ?
Set existingRow = tbl.ListRows(1)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.