I'm trying to use vlookup to read a string from a word document and return corresponding values from an excel sheet. I seem to be getting a "Subscript out or Range" Error on the vlookup function.
This is the worksheet that i am looking at: Sample Data
The code I have is:
Sub Autofill()
Dim oExcel As New Excel.Application
Dim testdb As Excel.Workbook
Dim testvar1 As Double
Set testdb = oExcel.Workbooks.Open("k:\SIF\Vibration\Dbase.xlsm")
testvar1 = oExcel.WorksheetFunction.VLookup("Roger", testdb.Sheets("Main").Range("A1:C4"), 2, False)
MsgBox (testvar1)
End Sub
I tried using the Application.Vlookup function as well, to no avail. Got the same error.
Main
doesn't exist in the Dbase.xlsm file. Maybe there's an extra space or something, check for accuracy. I emulated your code and it appears ok. One thing that might be an issue is if you are expecting to return a string, you may want to change testvar1 to be aString
and notDouble
. – Ryan WildryVlookup
returns an error (since not found), you need to add error handling in cases yourVlookup
is unsuccessful, see link : stackoverflow.com/questions/18063214/… – Shai Radoactivate
the excel window after open() and before .vlookup. based on my experience – Rosetta