Wednesday, September 3, 2008

Using Excel Functions in VBA - vlookup

Say we have a Sheet1 with following data :

COL A COL B
bat ball
cat mouse
zebra crossing

To look up content of COL B for a particular value of COL A, V for vertical Vlookup function can be used.

Sub func_vlookup()

Workbooks("workbookname.xls").Activate
Worksheets("Sheet1").Activate

findthis = "cat"
in_range = Range("A1:B3")
rtn_from_col# = 2 ' indicates from which column value is being returned

MsgBox WorksheetFunction.vlookup(findthis, in_range, rtn_from_col#)
'pops up "mouse"

End Sub

Match function can be written similarly. Try it!