• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Using dynamic reference with structured references in formula

sccowl

New Member
Is there a way to pass a value into formula that references a table, without using INDIRECT function?

I have a table set up with multiple columns, one per employee and a row per day, for example

DateWeeknumAlfieBertCharlie
1/1/20191011
2/1/20191110
3/1/20191111

I currently use a formula using the structured table references like

=sumifs(emp_table[Alfie],emp_table[[Weeknum]:[Weeknum]],$A2) where A2 contains the weeknum I want to sum.

I can drag copy this formula which automatically updated the second column to Bert, third to Charlie etc and gives the correct result. (Tip - Using Ctrl+D over the range also works great)

The formula works fine, but is a little inflexible as it requires a different formula in each column as it needs to reference a specific column name. This is likely to go wrong over time if and when its maintained by someone else.

Ideally, rather than referencing the column name emp_table[Alfie] I'd rather make this dynmaic and pass in a parameter, say emp_table[B$1] where B1 contains a name like 'Alfie'. I would then be able to use one common formula across all columns, so could be confident it would always be referncing the correct data so long as the value in B1 is correct.

It is possible to do this using INDIRECT formula such as =sumifs(indirect("emp_table["&B$1&"]"),emp_table[[Weeknum][Weeknum]],$A2) , but I don't want to introduce a volatile formula into large complex sheet.

It also needs to be done in a standard sheet, I can't use any VBA.

Thanks for any advice or ideas.
 
If 'Alfie' was in B1, you can INDEX the column you need via something like
=INDEX(emp_table, , MATCH($B$1, emp_table[#Headers], 0))

Plugging that into your original formula
=sumifs(INDEX(emp_table, , MATCH($B$1, emp_table[#Headers], 0)),emp_table[[Weeknum]:[Weeknum]],$A2)
 
Perfect, thanks. I use this all over the place but couldn't see the wood for the trees!
 
One trick that might make the formulae a little more readable is to use a defined name in place of
=emp_table[[Weeknum]:[Weeknum]]
If 'weeknum' were used, the formula
= SUMIFS( INDEX( emp_table, , MATCH($B$1, emp_table[#Headers], 0) ), weeknum, $A2)
is more straightforward to read and 'weeknum' is absolute despite the fact that it refers to
=emp_table[Weeknum]
 
Back
Top