Hello
can anyone help me with how to write the excel vlookup function in vba
the function in excel looks like this
VLOOKUP(O2,'[Consolidated list of supplier.xls]Sheet3'!$A$5:$F$218,6,FALSE)
how can i get something similar working in a macro.
thanks
namrata

vlookup
typeMismatch
If you have a file C:\MyFolder\book1.xls open it's name will be book1.xls. It will not include the path.
Finch82
also,
when i replace the code as
Application.WorksheetFunction.VLookup(LookupValue, TableArray, RangeLookup, ColIndex)
the error changes to "can not find the vlookup function in the worksheetfunction class" but if i write
Application.VLookup(LookupValue, TableArray, RangeLookup, ColIndex)
the error changes to "1004:application defined or object defined error"
any clue ....
i have fully qualified the range also as follows
Set LookupValue = ThisWorkbook.Sheets("Data Sheet").Range("O2")
namrata
nicholas winterer
hello,
the whole code that i have written is as follows
Public LookupValue As Range
Public TableArray As Range
Public MyPath As String
Sub VlookUp_Click()
MyPath = "C:\Documents and Settings\np22331\Desktop\Consolidated list of supplier.xls"
Set LookupValue = Sheet3.Range("O2")
Set TableArray = Workbooks(MyPath).Worksheets("Sheet3").Range("$A$5:$F$281")
End Sub
namrata
Ralf.B
Set LookupValue = Sheet3.Range("O2")
The 1004 error will be raised if not match is found. Are you sure the value you are looking for is in the lookup table
Alexei Sobolev
thanks Andy for the reply.
i tried writing something similar to what you suggested but i get error. i wrote
Public LookupValue As Range
Public TableArray As Range
Set LookupValue = Sheet3.Range("O2")
Set TableArray = Workbooks(MyPath).Worksheets("Sheet3").Range("$A$5:$F$281") ............... error here
The error is
Run Time error 9
Sub-Script out of range
can you please help
thanks
namrata
GeoffNin
Namrata,
Is MyPath the name of the workbook, or is it a variable If it is the name of a workbook, then put "MyPath" in quotations.
Thebitter
hello all,
MYPath is a variable which contains the name of the excel file i want to open for vlookup. it is in a different folder and not in the one where the original excel is (the one i am writing macro in).
can you now help me with why this error.
namrata
TimmyG15
Sheet3.Cells(2, 16) = Application.WorksheetFunction.VLookup(LookupValue, TableArray, RangeLookup, ColIndex)
ActiveWorkbook.Close SaveChanges:=False
Ephi
hi,
i modified the code as below but it gives the following error ..
run time error 1004
unable to get the vlookup property of the worksheet function class
my new code looks something like this.
**************************************************************************
Sub VlookUp_Click()
Dim LookupValue As Range
Dim TableArray As Range
Dim ColIndex As Integer
Dim RangeLookup As Boolean
Dim MyPath As String
MyPath = "C:\Documents and Settings\np22331\Desktop\Consolidated list of supplier.xls"
Workbooks.Open Filename:=MyPath, UpdateLinks:=0, ReadOnly:=True
Set TableArray = Workbooks("Consolidated list of supplier.xls").Worksheets("Sheet3").Range("$A$5:$F$281")
ActiveWorkbook.Close SaveChanges:=False
Set LookupValue = Sheet3.Range("O2")
RangeLookup = False
ColIndex = 6
Sheet3.Cells(2, 16) = Application.WorksheetFunction.VLookup(LookupValue, TableArray, RangeLookup, ColIndex)
End Sub
**************************************************************************************
can somebody please help me with fixing the bug !!
thanks
namrata
Spankmaster79
Rabtok
SteffoS
How about some like this
Dim rngLookupValue As Range
Dim rngtable As Range
Dim lngColIndex As Long
Dim blnRangeLookup As Boolean
Set rngLookupValue = Range("O2")
Set rngtable = Workbooks("Consolidated list of supplier.xls").Worksheets("Sheet3").Range("$A$5:$F$218")
lngColIndex = 6
blnRangeLookup = False
vntResult = Application.WorksheetFunction.VLookup(rngLookupValue, rngtable, lngColIndex, blnRangeLookup)
xinz
Any chance of emailing an example set of workbooks
shauli
MyPath = "C:\Documents and Settings\np22331\Desktop\Consolidated list of supplier.xls"
MyFile = "Consolidated list of supplier.xls"
Set LookupValue = Sheet3.Range("O2")
Set TableArray = Workbooks(MyFile).Worksheets("Sheet3").Range("$A$5:$F$281")