Is it possible in Excel VBA to reference a named table?
Hypothetically this could be...
Sheets("Sheet1").Table("A_Table").Select
I have seen some mention of tables being a list object but I'm not sure if that is the same thing...
Is it possible in Excel VBA to reference a named table?
Hypothetically this could be...
Sheets("Sheet1").Table("A_Table").Select
I have seen some mention of tables being a list object but I'm not sure if that is the same thing...
In addition, it's convenient to define variables referring to objects. For instance,
You will probably find it advantageous at once.
Maybe this can help you
Creating a table
Converting a range to a table starts with the same code as in Excel 2003 (as described in this answer):
A "table" in Excel is indeed known as a ListObject.
The "proper" way to reference a table is by getting its ListObject from its Worksheet i.e.
SheetObject.ListObjects(ListObjectName)
.If you want to reference a table without using the sheet, you can use a hack
Application.Range(ListObjectName).ListObject
.NOTE: This hack relies on the fact that Excel always creates a named range for the table's DataBodyRange with the same name as the table. However this range name can be changed...though it's not something you'd want to do since the name will reset if you edit the table name! Also you could get a named range with no associated ListObject.
Given Excel's not-very-helpful 1004 error message when you get the name wrong, you may want to create a wrapper...
Also some good ListObject info here.
In addition to the above, you can do this (where "YourListObjectName" is the name of your table):
But I think that only works if you want to reference a list object that's on the active sheet.
I found your question because I wanted to refer to a list object (a table) on one worksheet that a pivot table on a different worksheet refers to. Since list objects are part of the Worksheets collection, you have to know the name of the worksheet that list object is on in order to refer to it. So to get the name of the worksheet that the list object is on, I got the name of the pivot table's source list object (again, a table) and looped through the worksheets and their list objects until I found the worksheet that contained the list object I was looking for.
Maybe someone knows a more direct way.
The OP asked, is it possible to reference a table, not how to add a table. So the working equivalent of
would be this statement:
or to select parts (like only the data in the table):
For the parts, you may want to test for the existence of the header and totals rows before selecting them.
And seriously, this is the only question on referencing tables in VBA in SO? Tables in Excel make so much sense, but they're so hard to work with in VBA!