Use the Excel Vlookup Function in VBA

Application.WorksheetFunction.Vlookup ( lookup_value, table_array, col_index_num, [range_lookup] )


VBA Vlookup Example

Spreadsheet of Employee Hourly Pay Rates Used in VBA Vlookup Example
The following simple VBA function receives an employee name and a range of cells that contain rates of pay, as shown in the example spreadsheet on the right.
The VBA code calls the Worksheet Vlookup function, and uses this the look up the rate of pay for the supplied employee name.
For example, if the supplied employee name was "Benson, Paul" and the supplied Hourly_Rates range was columns A-B of the spreadsheet on the right, the VBA Vlookup would return the value $32.00.

' Function to look up the rate of pay for a supplied employee name and a supplied range of cells
' containing hourly pay rates

Function Pay_Rate( Employee_Name As String, Hourly_Rates As Range ) As Currency

    ' Call the Vlookup function to look up the rate of pay for the supplied employee name
    Pay_Rate = Application.WorksheetFunction.VLookup( Employee_Name, Hourly_Rates, 2, False )

End Function

Comments

Popular posts from this blog

50 Excel VBA Oral Interview Questions