I am kind of stumped on this one.
I have a single worksheet that has a number of different tables.
It also has a number of different formulas that reference data inside of those tables.
I had a problem where if I combined multiple workbooks, all of which contained the same tables, then my formulas would be incorrect. Reason being that tables of the same name get auto-renamed when combined into the same workbook.
So I thought I had a workaround for that.
The workbook/worksheet is a template and I have to put in a unique number in cell A1. The number will never be repeated amongst all the workbooks/worksheets, even when combined.
I figured, I will just auto-rename all the tables to contain the unique number, that way the formulas auto-update, the named ranges auto-update, and the tables will be unique, even when multiple worksheets are combined into a single workbook.
So, when I input a number in cell A1, using worksheet_change, I have all tables be renamed to tblname_number.
Example: I input 552843 in cell A1. The table name changes from "Fruit" to "Fruit_552843". The table "Veggies" changes to "Veggies_552843". The table "Food" changes to "Food_552843". etc etc etc.......
The code works great!!
And, when I clear out cell A1, then the tables auto-rename back to being "Fruit", "Veggies", "Food", etc......
Love it!!
However, I have several lines of code, modules, etc., that reference the exact table name and column in the table.
Because they are lines of code with a hard coded table name, when the table name auto-changes based upon a number input, the lines of code no longer function correctly.
Example:
ws.Range("Fruit[Apples]")
That line of code will not auto update to correctly reference the auto-named tables.
Question: Is there a way to reference a dynamic table name in a hard line of code? This is where I am a bit shaky.
I was thinking something like: ws.Range("Fruit & "_" & ws.Range("A1").value[Apples]")
So that basically it would give me: ws.Range("Fruit_12345"[Apples]")
I realize that the syntax probably isn't correct, but I think it portrays the idea clearly .... right?
My only concern about doing that is, if I clear out cell A1 from a previously input number, then it would reference table "Fruit_", when the real name of the table (when there is no number in cell A1) is "Fruit". So the line of code would still be broken.
I guess that I could correct my auto-rename of the tables to not include the underscore ("_"), so that it would be "Fruit12345".
But then how do I identify where to remove the numbers from the original table name when I clear out cell A1, so that it reverts back to "Fruit"?
I am currently using SPLIT and using the underscore as the separator.
Any thoughts, ideas, etc?
-Spydey
I have a single worksheet that has a number of different tables.
It also has a number of different formulas that reference data inside of those tables.
I had a problem where if I combined multiple workbooks, all of which contained the same tables, then my formulas would be incorrect. Reason being that tables of the same name get auto-renamed when combined into the same workbook.
So I thought I had a workaround for that.
The workbook/worksheet is a template and I have to put in a unique number in cell A1. The number will never be repeated amongst all the workbooks/worksheets, even when combined.
I figured, I will just auto-rename all the tables to contain the unique number, that way the formulas auto-update, the named ranges auto-update, and the tables will be unique, even when multiple worksheets are combined into a single workbook.
So, when I input a number in cell A1, using worksheet_change, I have all tables be renamed to tblname_number.
Example: I input 552843 in cell A1. The table name changes from "Fruit" to "Fruit_552843". The table "Veggies" changes to "Veggies_552843". The table "Food" changes to "Food_552843". etc etc etc.......
The code works great!!
And, when I clear out cell A1, then the tables auto-rename back to being "Fruit", "Veggies", "Food", etc......
Love it!!
However, I have several lines of code, modules, etc., that reference the exact table name and column in the table.
Because they are lines of code with a hard coded table name, when the table name auto-changes based upon a number input, the lines of code no longer function correctly.
Example:
ws.Range("Fruit[Apples]")
That line of code will not auto update to correctly reference the auto-named tables.
Question: Is there a way to reference a dynamic table name in a hard line of code? This is where I am a bit shaky.
I was thinking something like: ws.Range("Fruit & "_" & ws.Range("A1").value[Apples]")
So that basically it would give me: ws.Range("Fruit_12345"[Apples]")
I realize that the syntax probably isn't correct, but I think it portrays the idea clearly .... right?
My only concern about doing that is, if I clear out cell A1 from a previously input number, then it would reference table "Fruit_", when the real name of the table (when there is no number in cell A1) is "Fruit". So the line of code would still be broken.
I guess that I could correct my auto-rename of the tables to not include the underscore ("_"), so that it would be "Fruit12345".
But then how do I identify where to remove the numbers from the original table name when I clear out cell A1, so that it reverts back to "Fruit"?
I am currently using SPLIT and using the underscore as the separator.
Any thoughts, ideas, etc?
-Spydey