VBA insert an entire row at the last table row

MrVBAConfused

New Member
Joined
Jul 29, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Please help!
I have inherited a spreadsheet with some macros already created and i want to amend one to help with some other functions:

I would like to add a complete worksheet row but at the bottom of the table:

So the current macro adds a line at the bottom of a table (so far so good)
Code is:
Dim tbl As ListObject
Dim lastRow1 As Long
Set tbl = ActiveSheet.ListObjects("XYZ")
tbl.ListRows.Add
lastRow1 = 1

However, this only inserts a row in the table not the complete row.
I have know the code to insert a full row:
Code is:
ActiveCell.EntireRow.Insert

But, this requires a cell/ range to be chosen or specified. unfortunately my table is dynamic and constantly changing.

there must be some way to select the table, then select the last row then just insert entire row. (this is probably way too easy for you guys! sorry!!)

Cheers
Martin
 

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)
Welcome to the MrExcel board!

Not absolutely certain where you want the row added, but see if this is close.

VBA Code:
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("XYZ")
With tbl.Range
  Rows(.Row + .Rows.Count).Insert
End With
 
Upvote 0
Hi Peter,

Thank you.

this works but adds the row under the last table row. is it a case of adding a shift function or saying 1 up?

Cheers
Martin
 
Upvote 0
Welcome to the MrExcel board!

Not absolutely certain where you want the row added, but see if this is close.

VBA Code:
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("XYZ")
With tbl.Range
  Rows(.Row + .Rows.Count).Insert
End With

Just realized i didn't reply directly, sorry.

Also added a screen shot to help:

In the screenshot, the yellow cells make up the table "XYZ" when using the code you gave above, this inserted a row, but below the Total line (row 11 in this instance) and not between row 9/10
 

Attachments

  • Table Row issue.JPG
    Table Row issue.JPG
    45.6 KB · Views: 150
Upvote 0
How about
VBA Code:
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("XYZ")
With tbl.ListRows
   .Item(.Count).Range.Offset(1).EntireRow.Insert
End With
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0
Thinking about it, as you have a totals row, the code can be simplified like
VBA Code:
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("xyz")
tbl.TotalsRowRange.EntireRow.Insert
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,697
Members
449,117
Latest member
Aaagu

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