I'd like to be able to retrieve values from an Excel table by row number and column name (for code readability and robustness).
In formulas, I can use Structured References with column header text and get a value from the table like this:
=INDIRECT(ADDRESS(<absolute_line_number>;COLUMN(<table_name>[<column_name>])))
This is robust for formulas because if the user renames the column, all Structured References to it in formulas will be automatically updated.
Not so for VBA code though.
For worksheets, it's possible to define Worksheet.CodeName
for use in VBA code that will stay the same if the user renames the visible sheet name.
No such property exists for an Excel table AFAICS.
The best idea I currently have is to make table headers 1-cell Named Ranges. Then I can get a value from a table in VBA like this:
<sheet_codename>.Cells(<line_number>,Range("<range_name>").Column)
This, however, bothers me because Named Ranges are disconnected from the table. E.g. if I rearrange tables on the sheet, the ranges will remain in the old place.
Is there a better option? "Better" means specifically:
- Survives renaming and/or rearranging columns in the table, moving the table at least within the sheet