I have values from cells A1 to A20. I want to Vlookup
each of them to get corresponding values from a closed Excel file. I found some ways that work, but I don't want to open any file even it stays hidden and don't want to write the Vlookup
as a formula in any cell.
I tried Application.WorksheetFunction.VLookup
and ExecuteExcel4Macro
methods.
1 - This is a working example: ExecuteExcel4Macro to get value from closed workbook, but I wasn't able to alter this to work with Vlookup
.
wbPath = "c:\users\fatihmi\Desktop\"
wbName = "Ornek.xlsx"
wsName = "Sheet1"
MsgBox ExecuteExcel4Macro("VLOOKUP(" & "testString" & ";" & "'" & wbPath & "[" & wbName & "]" & wsName & "'!$C:$E;3;FALSE)")
2 - I don't know how to reference or use a closed document with Application.WorksheetFunction.VLookup
.
Dim wk As Workbooks
Set wk = "c:\users\fatihmi\Desktop\Ornek.xlsx"
Dim ws As Worksheet
Set ws = wk.Sheets("Sheet1")
Dim wr As Range
Set wr = ws.Range("C:E")
result = Application.WorksheetFunction.VLookup("testString", wr, 3, False)
MsgBox result
There is a possibility to get range with InputBox
, but I don't know how to use raw range data as in code with Application.WorksheetFunction.VLookup
.
Application.InputBox(prompt:="Enter range", Type:=8)
You have made a few mistakes with the
ExecuteExcel4Macro
. It should work when:,
isntead of;
)Some information about the above can be found here
You can check the output of the following:
So the code should be:
Edit
Might you have 1000's of rows, I'm sure using
INDEX
andMATCH
combo will be faster. In that case the code might look a little longer but should be faster: