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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,215,262
Messages
6,123,935
Members
449,134
Latest member
NickWBA

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