Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a VLOOKUP function in vba

Tags:

excel

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) 
like image 938
Mike Avatar asked Apr 06 '11 13:04

Mike


People also ask

Can you use a VLOOKUP in a macro?

VLOOKUP is one of the most useful and versatile functions in Excel. As you work further with macros it's not uncommon to make your create an Excel VBA VLOOKUP macro. With this you get the ability to reference your tables of data, but automated.


1 Answers

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) 
like image 73
Ben Hoffstein Avatar answered Oct 27 '22 02:10

Ben Hoffstein