VBA: Insert row before the last row of a table

Usually_Wrong

New Member
Joined
Oct 6, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Greetings,

I have a table ("Position") that resides within a named range ("Test"). Currently, the named range is for A1:G9, or the green border surrounding the table.

1656003801807.png


I have the code that inserts a row to the table; however, my issue is that this doesn't make the named range dynamic. I am hoping to modify this code to not insert on row 9 (bottom of the table), but above row 8 instead so the named range expands with the row insertions.

VBA Code:
Sub InserttoTable()
     Dim oSheetName As Worksheet
     Dim sTableName As String
     Dim loTable As ListObject

     sTableName = "Position"

     Set oSheetName = Sheets("Sheet1")

     Set loTable = oSheetName.ListObjects(sTableName)

     loTable.ListRows.Add
End Sub

Any help is appreciated. Thank you!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Count the number of rows in the table "Position" before adding.
Replace last line of code with this.
VBA Code:
vLRC = oSheetName.ListObjects(sTableName).ListRows.Count
oSheetName.Rows(vLRC + 2).Insert
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,058
Members
452,542
Latest member
Bricklin

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