How to use 2 cells values to create the name of a table you wish to look extract a value from

StackOverflow https://stackoverflow.com/questions/23631602

  •  21-07-2023
  •  | 
  •  

Question

EDIT *

    A        JAN      FEB      MAR      APR 
1<drpdwn>    A1+JAN   A1+FEB   A1+MAR   A1+APR

what i need is for B1 to hold a formula that will take A1+B as a string and use this as the name of a Table to grab a value from. so something like

=INDIRECT($A1&"B")[[#Totals],[Column7]] so the cell will display the value in column 7 in the totals row for a table that is named the result of '=INDIRECT($A1&"C")' =B2&A3 also works for creating the variable, but again. unable to use in the above formula

Hopefully this is a little bit clearer

As an example, I will have 12 tabs that lets say are months, JAN FEB MAR etc... these are the column headers in my above. Within these will be various Tables, like Expenditure, income, etc... they will be named ExpenditureJAN, ExpenditureFEB.

I want a simple (overview) tab, that lets me select expenditure in A1, and each months columns will use the formula to display a value from its relevant table, so A(JAN) would display jan's expenditure total etc.....

=ExpenditureJAN[[#Totals],[Column7]] this would work, but i need the expenditure bit, to change to what ever is selected from a picklist in A1.

***Old Question - Before edit - Didn't know how to word the question, but here is my problem; I have tables called AB, AC, AD, BB, BC etc... I want to look up the following =AB[[#Totals],[Column7]] which works fine. However..... I want it to change AB to be the string created from two cells so for instance =A3&B2[[#Totals],[Column7]] Hopefully that makes sense, i am struggling to put this into words for some reason.... To add to this, A3 will be a drop down list, so the idea is, i change the value in A3 and it will change the table each cell the formula is in to look at the correct value. B C D E # #B #C #D #E So # can change via a picklist, and the formula will detect this change and fire off to look at #totals, column 7 in the table #B/#C etc.. using the column headers and picklist as references for the table name.*

Was it helpful?

Solution

As user3514930 stated you want to use the INDIRECT function. Given the following table:

    A        B    C    D    E
1<drpdwn>    #B   #C   #D   #E

If Cell B1 contains the formula =INDIRECT($A1&"B") it would show whatever is selected in cell A1 immediately followed by the character B. So drag that over to fill the rest of the row and update the trailing letter to =INDIRECT($A1&"C"), =INDIRECT($A1&"D"), =INDIRECT($A1&"E") etc. in the formulas.

Then when the dropdown box in cell A1 is changed, say to C the table would look like this:

    A    B    C    D    E
1   C    CB   CC   CD   CE

Edit: So I got this to work like you wanted using INDEX, INDIRECT, and an additional cell to reference the table desired. I setup a table like you suggested named ExpendituresJAN. It had 6 columns, first was Week1-Week4, then HR, IT, Admin, Sales, and Payroll for departments, and then Column7 where I had totals per week. I threw in some random numbers (IT had a heck a week 3, spending nearly $997k evidently, and the sales department outdid itself with it's end of month party running over $10k, but those boys do love to party!). Sorry, where was I? Oh yes, example table, I added a Totals row on, and went to trying to figure it out because INDIRECT really seemed to be the key and I just had to wrap my head around things.

Next sheet had basically 4 cells with anything in them. A2 had a dropdown where I could Choose JAN, FEB, or MAR. B1 said JAN (I imagine C1 would say FEB, but I didn't go that far). B2 said #REF! a lot of the time.

________| JAN
<drpdwn>| #REF!

In the end A2 was moved down to A3 for my dropdown box, and row 2 was hidden, I'll get to why that is in a second. Ok, how to reference that... I can't seem to do it traditionally with structured reference, which is a pain, but there's got to be a way around it. I crack my knuckles, pull up Chrome, and practice a little Google-fu. Turns out what we needed was INDEX, which will return a cell or range from a table by name. B2 (hidden now) now contains the formula ="Expenditures"&A3 which, when JAN was selected read ExpendituresJAN. Excellent, I'm pretty sure we're most of the way there now. I just have to phrase my INDEX right and I'm set.

So, INDEX ended up being relatively simple all in all, but there's a catch. No #Totals row accessibility for INDEX. I got hung up on that for a few minutes until I stepped back and thought "wait, what's my totals row except a =SUM() formula? Ok, I can duplicate that. INDEX works with this syntax: INDEX(array/table, Row#, Col#) but you can use 0 for the row and it takes the entire row into account. Perfect for what I want, since I want a sum of Column7, I'll just wrap the INDEX in a SUM function.

=SUM(INDEX(INDIRECT(B2),0,7))

That spits back $1,036,371.00, the exact same thing my totals row shows for Column7. The only complication I could see is if your different tables have different Totals rows, as in one does a SUM, one does an AVG, and what not. Then the formula starts getting a bit more complex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top