Dynamic table_array value for Vlookup function - excel

Alright, I am trying to figure out if the following is possible so that I can use it in later projects. I have been testing and working on this code in a sample workbook, which is why the file name is garbage. So don't judge me.
I have a VLOOKUP:
=VLOOKUP(A6,[dfhdfh.xlsx]Sheet1!$A:$B,2,FALSE)
This function currently works great. But I want to replace the static table_array value in the function to a cell reference, where I can enter a different file name at will.
Something along the lines of:
=VLOOKUP(A3,CONCATENATE("[",F2,"]","Sheet1!$A:$B"),2,FALSE)
Where F2 contains the file name and extension dfhdfh.xlsx
But whenever I try to execute my VLOOKUP with it's nested CONCATENATE function, I get a #VALUE! error. What gives?

Follow up from comments
If your workbook dfhdfh.xlsx is always open, you can use
=VLOOKUP(A3,INDIRECT("["&F2&"]Sheet1!$A:$B"),2,0).
But if your wb is closed, INDIRECT doesn't work. In that case you need VBA solution.
About your formula:
1) CONCATENATE(A1,A2) is the same as A1 & A2.
2) Actually concatenation works and result of CONCATENATE("[",F2,"]","Sheet1!$A:$B") would be "[dfhdfh.xlsx]Sheet1!$A:$B", but excel doesn't recognize this string as reference.
So you need to use Indirect for this purpose:
INDIRECT(CONCATENATE("[",F2,"]","Sheet1!$A:$B")) gives you correct reference.
Entire formula would be:
=VLOOKUP(A3,INDIRECT(CONCATENATE("[",F2,"]","Sheet1!$A:$B")),2,FALSE).
But, using first point, you can make this formula shorter:
=VLOOKUP(A3,INDIRECT("[" & F2 & "]Sheet1!$A:$B"),2,FALSE)

Related

REF Error when using INDIRECT function to create dynamic row reference

I have a matrix grid in "MasterSheetGrid". I have separate sheets that divide this info into certain dimensions, making it easier to handle for the user.
In order to make the file dynamic, i am trying to use INDIRECT Function within a function, to locate which row of the MasterSheetGrid to look for the information before returning.
The formula works when i specify the row manually, but using INDIRECT i receive a REF error, even though nothing is deleted.
Manual Formula =INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),MasterSheetGrid!6:6,0))
Formula to locate the row
=(MATCH($C6,MasterSheetGrid!$C:$C,0))
Attempt to merge both using INDIRECT by referencing the cell where the above formula is stored, which results in REF
INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),(INDIRECT(J2:J2,0))))
Ideally i would like to not have to use a cell to store the lookup row formula in, but i thought if i could create a correct formula with the cell reference, i could repeat for the formula.
Does anyone know what i am doing wrong?
This is the view of the user. The formula would sit within column K
This is the MasterSheetGrid view
Instead of using INDIRECT which would cause recalculation at any change in the file, I recommend using INDEX instead. You refer to a fixed sheet name, therefore no need to use INDIRECT.
=INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),INDEX(MasterSheetGrid!$1:$50,J2,),0))
Would be the equivalent of what you tried.
Proper use of INDIRECT would be:
=INDEX(MasterSheetGrid!$5:$5,MATCH((XLOOKUP($J6,$5:$5,6:6)),INDIRECT("MasterSheetGrid!"&J2&":"&J2),0))
And it's good practice to take the following into account (thanks David Leal):
If the Sheet name you're referring to contains one or more spaces you need to wrap the name in ' like 'Sheet name'!
To refer a range using INDIRECT you can use the following syntax (as a string delimited, for example delimited by "):
=INDIRECT("J2:J10")
for a cell, this works:
=INDIRECT(J2)
but if you try the same for a range:
=INDIRECT(J2:J10) -> #REF!
you get #REF!
If you are going to refer a Sheet from your Worksheet, then you need in all cases to enter the input argument as string:
=INDIRECT("Sheet1!A1:A10")
=INDIRECT("Sheet1!A1")
=INDIRECT("'Sheet 1'!A2") -> When Sheet Name has spaces use (') delimiter
Notes: By the way you are invoking INDIRECT using the second input argument (a1) which is optional with the value 0. It is not required for getting a referring a range as I showed before.
I suspect this is the issue you are having

Excel INDIRECT Function Range not Valid

In a column, I´m storing ranges as plain Text.
I then want to use these ranges in a formula. As everything so far was in the same workbook I had no issue.
Now I want to get value from another workbook so I added just the path of the file just in front of my range.
It gives me something like that (stored in cell R38):
'C:\Users\me\Documents\C251\[C251output_powereditor.xlsx]C251!'G4:G38
Then I' m trying to use the following formula :
MATCH("Stlnr.";INDIRECT(R38);0)
But I got a ref error.
If I try the following :
MATCH("Stlnr.";[C251output_powereditor.xlsx]C251!G4:G38;0)
It does work.
I´m not sure what the issue is with my indirect function. And before you ask the other workbook is open. :)
Thanks in advance
I think your problem could be that when you enter
'C:\Users\me\Documents\C251[C251output_powereditor.xlsx]C251!'G4:G38
into a cell, Excel treats the first ' as the symbol of starting a text field, so it thinks the path is C:\Users\me\Documents\C251[C251output_powereditor.xlsx]C251!'G4:G38.
Solution: Add a single quote either in the formula or in the data cell:
''C:\Users\me\Documents\C251[C251output_powereditor.xlsx]C251!'G4:G38
or
MATCH("Stlnr.";INDIRECT("'"&R38);0)

How to evaluate the content of another cell in the same spreadsheet without any VBA or any other addon

With the cell A1 containing a formula saved as string. Ex: Sum(B1:B5)
In A2 I want to execute content of A1. But when I put =(=A1) in A2 excel gives me formula error. Is there any way I can execute content of A1 in A2 as formula
Mind you no VBA is allowed. Can someone please help?
Please only those people should answer who have done this thing in the past. No hit and tries please
=A1
that's all you need to point the value to the value in cell A1
With VBA :
with VBA you can write some custom function and then evaluate the formula.
Then call the function in excel wherever you required like below
=eval_formula(A1)
Function eval_formula(fr)
eval_formula = Evaluate(fr.Value)
End Function
Refer the link here for more details
https://superuser.com/questions/253353/excel-function-that-evaluates-a-string-as-if-it-were-a-formula
without VBA:
Create a named range and use the named range inside the cell, I have attached the screenshots for your reference
hope this is what you required.
The Evaluate function doesn't exist in Excel anymore.
The only way you can use to evaluate properly is unfortunately only VBA.
In addition to the good answer of Durgaprasad. Here are some other ways to evaluate in the related question:
How to turn a string formula into a "real" formula

How to reference external file by adding TEXT(TODAY()) into path

Preparing worksheets which will compare data from two columns. One of columns will be linked with archived files from prior day. This is my cell formula:
=VLOOKUP("Cash",'C:\...\"&TEXT(TODAY(),"yyyy")&"\"&TEXT(TODAY(),"mmmm")&"\"&TEXT(TODAY()-1,"dd")&"\[XXXX.xls]Check'!$L$10:$M$76,2,0)
Is this formula proper? Still returning N/A. When I put date manualy it works fine. Seems that problem is with TEXT() formula. Any idea?
You're doing it wrong. VLOOKUP() expects range reference as its second parameter. You are posting a reference to excel file named 'C:\...\"&TEXT(TODAY(),"yyyy")&"\"&TEXT(TODAY(),"mmmm")&"\"&TEXT(TODAY()-1,"dd")&"\[XXXX.xls]' because your formulas and & operators will not be evaluated inside apostrophes ''.
You need to wrap the string into INDIRECT() to evaluate it into reference:
=VLOOKUP("Cash",INDIRECT("'C:\...\"&TEXT(TODAY(),"yyyy")&"\"&TEXT(TODAY(),"mmmm")&"\"&TEXT(TODAY()-1,"dd")&"\[XXXX.xls]Check'!$L$10:$M$76"),2,0)
However, don't expect this to open the whatever filename it calculates and fetch the data into your cell — it will return #REF! unless the file it is targeting is not open in Excel right now.

Using indirect function in an IF statement with quotes

I'm making multiple IF statements that are going to have the same layout. Instead of writing the reference sheet name I'd like to reference a cell for the sheet name.
Also in the interests of laziness I'd like to drag the formula so it changes locations it is looking at on the referenced sheet.
At the moment it looks like this.
=IF(sheet1!O2="","",sheet1!O2)
Simple enough.
However I want to use indirect and I can't write it without getting an error.
Last attempt was
=IF((indirect($B$3))!O2="","",(indirect($B$3))!O2)
where Sheet1 is in the cell B3
Doesn't work.
Any help on the correct syntax would be very appreciated.
You need to concatenate $B$3 and "!O2" to generate "Sheet1!O2" as a string for INDIRECT to work, as below:
=IF(indirect($B$3&"!O2")="","",indirect($B$3&"!O2")

Resources