Forum Discussion

Gerry Poppe's avatar
Gerry Poppe
Copper Contributor
Mar 27, 2020
Solved

Dynamic cell reference to workbooks using IF statement

I have a workbook which contains multiple years of data.  Each year is in a separate worksheet.  I want to be able to use a dynamic link which would allow me to gather data from anyone of the workshe...
  • Riny_van_Eekelen's avatar
    Mar 28, 2020

    Gerry Poppe 

    First of all, I would include the single quotes and the exclamation mark when you concatenate the sheet name you want to call with the INDIRECT function. For example =CONCAT("'"&B1&" "&B2&"'!") to produce something like 'Sheet Name'! (where B1 contains "Sheet" and B2 contains "Name"). Better to do that once than every time you use INDIRECT. Then, the formula itself seems overly complicated for what you want to do.

     

    Your first VLOOKUP could be replaced by:

    IF(INDIRECT($E$1&"A28")=1

     

    and your second one by:

    IF(INDIRECT($E$1&"A28")=100

     

    Similarly, the parts directly after each IF statement can be simplified to: INDIRECT($E$1&"B28")

     

    But then, the logic of the formula says: If a value equals 1 then return the value in 'Sheet Name'!B28. If it is not then see if it equals 100 and if so, then also also return the value from 'Sheet Name'!B28". Otherwise, return 0.

     

    The same logic would also be achieved by this:

    =IF(OR(INDIRECT(E1&"A28")=1,INDIRECT(E1&"A28")=100),INDIRECT(E1&"B28"),0)

     

    If you insist to work off your original formula (with some small modifications), this one will work as well and it will have eliminated the #NA! error.

    =IF(IFERROR(VLOOKUP(1,(INDIRECT("'"&$E$1&"'!A28")),1,FALSE),0)=1,INDIRECT("'"&$E$1&"'!b28"),IF(IFERROR(VLOOKUP(100,INDIRECT("'"&$E$1&"'!A28"),1,FALSE),0)=100,INDIRECT("'"&$E$1&"'!b28"),0))

     

     

Resources