Writing a VLOOKUP function in vba
I'm trying to lookup a value on a spreadsheet within a table array using the VLOOKUP function in my vba code. I don't know how to write it correctly.
Here is the normal VLOOKUP formula with all the references:
=VLOOKUP(DATA!AN2,DATA!AA9:AF20,5,FALSE)
Have you tried:
Dim result As String
Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets("Data")
result = Application.WorksheetFunction.VLookup(sheet.Range("AN2"), sheet.Range("AA9:AF20"), 5, False)
How about just using:
result = [VLOOKUP(DATA!AN2, DATA!AA9:AF20, 5, FALSE)]
Note the [ and ].
Please find the code below for Vlookup
:
Function vlookupVBA(lookupValue, rangeString, colOffset)
vlookupVBA = "#N/A"
On Error Resume Next
Dim table_lookup As range
Set table_lookup = range(rangeString)
vlookupVBA = Application.WorksheetFunction.vlookup(lookupValue, table_lookup, colOffset, False)
End Function