I'm using the Index(Match()) function to lookup a value from another closed workbook. The formula within cell B5
of the currently opened workbook is:
=INDEX('C:\Users\Downloads\[og.xlsx]Sheet1'!$B$1:$B$3,MATCH($A$5,'C:\Users\Downloads\[og.xlsx]Sheet1'!$A$1:$A$3,0))
where $A$5
stores the lookup value.
The path of source file C:\Users\Downloads\[og.xlsx]Sheet1
will change dynamically, and it is stored in the current workbook in cell C1
as text string without '
at either ends. I would like to have some help writing a user defined function (not sub!) in vba to replace the fomula as:
=INDEX(getValue($C$1,"$B$1:$B$3"),MATCH($A$5,getValue($C$1,"$A$1:$A$3"),0))
NOTE:
- The source file should not open at any point, so
INDIRECT()
won't work. - Excel add-in is not preferred, so no
INDIRECT.EXT()
. - The
getValue(path,range)
function should also work whenrange
refers to a single cell value. (i.e.=getValue($C$1,"$B$1")
should work as a cell formula alone) - I noticed the post Use file path from a predefined cell in a formula , which works fine for single cell value but not range when using index(match()). Also, I tried the following which returns
#VALUE!
:
Function GetValue(Rpath As String, Rrange As String)
Dim a, strRef As String
a = range(Rrange).Address(, , xlR1C1)
strRef = "'" & Rpath & "'!" & a
GetValue = ExecuteExcel4Macro(strRef)
End Function
Thanks in Advance!