VBA macro to add row to table

bertilak

New Member
Joined
Jun 5, 2019
Messages
17
I got a great answer to my previous question (VBA copy from cell range to current location.) which was about how, using a VBA macro, to copy a range of cells to the current cell.

I would now like to improve upon this macro by having the current cell be automatically selected (by the macro) as the first cell below a table. This table automatically extends as new rows are appended to the end. Right now I need to manually select the cell then run the macro. It would be nice if I could automatically position the macro to the cell just below the table.

What I tried:

VBA Code:
Range(Table1).EntireRow.Insert
Table1.EntireRow.Insert
And some similar things. All get errors like
Runtime error '1004' Method 'Range' of object _global failed (first line above)
Runtime error '424' Object required (2nd line above)

Note that "Table1" is what the name manager shows and it's the onle table in thedocument.
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What columns is your table in?
If column A is the first column in your table, and it always has data for every row in your table, you can use this line to select the first blank row under it (assuming that there is nothing else already under the table):
Excel Formula:
Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Select
 
Upvote 0
Let me rephrase the question.

What I really want is a way to make the row just after the end of the table the active row. Then when I do the paste (as in the answer to my original question referenced above) the table should automatically extend to include the new row.
What columns is your table in?
If column A is the first column in your table, and it always has data for every row in your table, you can use this line to select the first blank row under it (assuming that there is nothing else already under the table):
Excel Formula:
Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Select
Hi. Thanks.

That worked but I'm not sure I understand it. There are blank rows above the table -- how come this doesn't confuse things?

Wouldn't it be more robust to somehow reference the actual table itself? It has a name ("Table1").
 
Upvote 0
Let me rephrase the question.

What I really want is a way to make the row just after the end of the table the active row. Then when I do the paste (as in the answer to my original question referenced above) the table should automatically extend to include the new row.

Hi. Thanks.

That worked but I'm not sure I understand it. There are blank rows above the table -- how come this doesn't confuse things?

Wouldn't it be more robust to somehow reference the actual table itself? It has a name ("Table1").
What line of code does is start at the very bottom of Excel, in the last possible row (Rows.Count will return the last possible row number in Excel).
By starting at the very bottom and going up, you find the last populated cell in that column (column A in our example).
Then Offset(1 , 0) just moves you one row down from that.

In summary, so what that line of code does is find the last populated cell in column A and then move down to the row underneath that.
As long as you don't have anything under/below your table in column A, this should do what you want.
 
Upvote 0
Wouldn't it be more robust to somehow reference the actual table itself? It has a name ("Table1").
There are often multiple ways of doing things in VBA and they all have their strengths and weaknesses.
Here is an option that uses the table name.

VBA Code:
Sub TestCopyNewRows()

    Dim lo As ListObject
    Dim newRow As ListRow
    Dim cpyRng As Range
    
    Set cpyRng = Range("I6:J9")
    Set lo = Range("Table1").ListObject
    Set newRow = lo.ListRows.Add
    
    cpyRng.Copy Destination:=newRow.Range.Cells(1)
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,216
Members
449,091
Latest member
jeremy_bp001

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