How to get the name of a table in a variable?

TheBlueMusketeer

New Member
Joined
Sep 29, 2012
Messages
12
Hello,

Any ideas on how to capture the name of a table in a variable?

To add a table, we use the following code (recorded it) :
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$D$4:$J$16"), , xlNo).Name = "Table1"

I need to refer to a table from a sheet that gets updated periodically but sometimes the same table download ends with a different number - "Table2" for example, and my vlookup formula breaks. Therefore I want to use a variable that stores the table name from this particular sheet every time the macro is run.

Thanks,
TBM
Excel 2007
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Is there always one and only one table on the sheet? Try

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ReferToTable_v1()<br>  <SPAN style="color:#00007F">Dim</SPAN> TableName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><br>  TableName = Sheets("Table Sheet").ListObjects(1).Name<br>  MsgBox TableName<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


If there could be more than one table but you know where the table of interest is (J1 in my example) then try

<font face=Courier New><br><br><SPAN style="color:#00007F">Sub</SPAN> ReferToTable_v2()<br>  <SPAN style="color:#00007F">Dim</SPAN> TableName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>  <br>  TableName = Sheets("Table Sheet").Range("J1").ListObject.Name<br>  MsgBox TableName<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks a bunch Peter! It worked. There is only one table in my sheet but now I know what to do when there are more. Thanks again!

Regards,
TBM
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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