Adding row in a table when the name is not defined

WxShady13

Board Regular
Joined
Jul 24, 2018
Messages
151
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello. I am using a Macro to create a worksheet that contains a table. The name of the table is PayData, however since this table is on every worksheet (individual employees) Excel gives it a name such as PayData28. How can I use the below VBA code to identify the PayData table that is on the worksheet properly?
VBA Code:
Sub AddRow()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("A9")
'add a row at the end of the table
tbl.ListRows.Add
'add a row as the fifth row of the table (counts the headers as a row)
End Sub
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,886
Office Version
  1. 365
Platform
  1. Windows
If you only have one table on each sheet, you could use
VBA Code:
Set tbl = ws.ListObjects(1)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,886
Office Version
  1. 365
Platform
  1. Windows
Does the pay table always start in the same cell on every sheet?
 

WxShady13

Board Regular
Joined
Jul 24, 2018
Messages
151
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile

ADVERTISEMENT

Does the pay table always start in the same cell on every sheet?
Yes the header row is always A9
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,886
Office Version
  1. 365
Platform
  1. Windows
In that case you can use
VBA Code:
Set tbl = Range("A9").ListObject
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,886
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,437
Messages
5,596,130
Members
414,043
Latest member
thomas Stein

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
Top