Range("F3").Value = WorksheetFunction.VLookup(Range("E3").Value, Range("A2:B100"), 2, False) looks up the value in E3 in the first column of A2:B100 and returns the matching value from the second column. False asks for an exact match; leave it out and VLOOKUP assumes sorted data and returns an approximate match.
When the value isn’t found, WorksheetFunction.VLookup stops the macro with run-time error 1004. Use Application.VLookup with a Variant and test the result instead:
Dim result As Variant
result = Application.VLookup(Range("E3").Value, Range("A2:B100"), 2, False)
If IsError(result) Then result = "Not found"
Range("F3").Value = result
To fill a whole column, write the formula into the range in one step, then convert it to values if it shouldn’t stay live: Range("F3:F50").Formula = "=VLOOKUP(E3,$A$2:$B$100,2,FALSE)", then Range("F3:F50").Value = Range("F3:F50").Value. Excel adjusts E3 for each row.
In Excel 365 or 2021, WorksheetFunction.XLookup can look to the left and takes a not-found value as its fourth argument: WorksheetFunction.XLookup(Range("E3").Value, Range("A2:A100"), Range("B2:B100"), "Not found").
A number stored as text won’t match a real number, so 1001 in the lookup cell won’t find “1001” in the table. Convert the lookup value with CLng or CStr to match the table, or fix the column with Data → Text to Columns → Finish.
This tutorial will teach you to use VLOOKUP in VBA code. Make sure to read the entire article, or you can also watch the video tutorial.

How to Use VLOOKUP in VBA?
If you want to use VLOOKUP in VBA, you need to use the WORKSHEETFUNCTION property, which allows you to access the worksheet functions in VBA. When you type WorksheetFunction followed by a dot, you get the list of the functions, and you can choose VLOOKUP from there.

In this example, we have a list of students where we have the name and the score. We need to write a code with VLOOKUP to get the student’s score by using the name as a lookup value.
Steps to Write a VBA Code to Use VLOOKUP in Excel
- First, enter the WorksheetFunction property and press the dot (.) to get the function list.
- Next, enter VLOOKUP or select it from the list.
- Now, enter the opening parenthesis to start the function.
- After that, specify the arguments of the VLOOKUP function. (Later in this post, I have shared detailed steps to specify the arguments.)
- In the end, specify a cell (with a range object) to get the result of the function.
Range("F3") = WorksheetFunction.VLookup(Range("E3"), Range("A:B"), 2, False)

Define Arguments in the VLOOKUP in VBA
When you use a worksheet function in VBA code through the WorksheetFunction property, VBA won’t show you the names of the arguments you need to define. It only shows them as Arg1, Arg2, Arg3, and Arg4.

That’s why you need to know the arguments you must specify in the function.

In VLOOKUP, there are four arguments to define:
- In the first argument, you need to define the value which you want to look for in the data. In our example, we need to define the cell with the value, so we have used a range object to specify the cell E3.
- In the second argument, you need to define the table (data) from which you want to look for the value. And we have again used a range object to specify range A:B.
- In the third argument, we need to define the column index number, which tells the function the column number from which we need to get the result value. In our example, we have specified 2.
- In the fourth argument, we need to define the match type (exact or approximate). As we need to do an exact match, we have used FALSE.
If you use TRUE in the fourth argument (or leave it out), VLOOKUP does an approximate match. For that, the values in the first column of the table must be sorted in ascending order; otherwise, you can get a wrong result. That’s why, for names and IDs, always use FALSE.
Once you specify all the arguments, you need to get the result of the function in a cell or through a message box. That’s why, in the code, we specified the cell F3 to get the value.
Using VLOOKUP in VBA for Multiple Values
If you want to apply VLOOKUP to get results for multiple values, you can use code like the following.
Sub vba_vlookup() Dim i As Integer Dim lookup_start As Range Set lookup_start = Range("E2") i = 5 For i = 1 To i lookup_start.Offset(i, 0) = WorksheetFunction.VLookup(lookup_start.Offset(i, -1), Range("A:B"), 2, False) Next i End Sub
In the above code, we have used the FOR NEXT LOOP to loop the VLOOKUP for five cells downwards.

In our data, we have five values for which you need to look up the score. These values are in the range D3:D7, and we need to get the score in the range E3:E7.
This code starts from the cell E2, uses Offset to go to the cells from E3 to E7, and uses VLOOKUP to get the score for your values in the corresponding cells (column D). As we have five values for the lookup, we have used 5 in the loop counter. In the first round of the loop, Offset(1, 0) takes the code to E3, and the next four rounds fill E4:E7.
There’s one catch with this code: if any of the values in column D is not in the data, WorksheetFunction.VLookup stops the code with an error. In the next section, I have shared how to handle that.
Handle the Error When VLOOKUP Can’t Find a Value
In a worksheet, VLOOKUP returns #N/A when it can’t find the lookup value. But in VBA, WorksheetFunction.VLookup doesn’t return #N/A. It stops the code with run-time error 1004 (“Unable to get the VLookup property of the WorksheetFunction class”). You get the same error when the column index number is bigger than the number of columns in the table.
To handle this, use Application.VLookup instead of WorksheetFunction.VLookup. It takes the same four arguments, but when there’s no match, it returns an error value instead of stopping the code. You can store that in a Variant variable and test it with IsError.
Sub vlookup_not_found()
Dim result As Variant
result = Application.VLookup(Range("E3").Value, Range("A:B"), 2, False)
If IsError(result) Then
Range("F3").Value = "Not found"
Else
Range("F3").Value = result
End If
End Sub
Application.VLookup doesn’t show up in the list of functions when you type the dot, but it works the same way. Make sure to declare the result variable as Variant; with a String or a number variable, you get a “Type mismatch” error when the value is not found.
You can also keep WorksheetFunction.VLookup and catch the error with On Error. But Application.VLookup with IsError is simpler, and it doesn’t hide other errors in your code.
Show the VLOOKUP Result in a Message Box
Instead of a cell, you can also show the result in a message box. In the following code, an input box asks you for the student’s name, and then a message box shows the score.
Sub vlookup_msgbox()
Dim student As String
Dim score As Variant
student = InputBox("Enter the student name")
If student = "" Then Exit Sub
score = Application.VLookup(student, Range("A:B"), 2, False)
If IsError(score) Then
MsgBox student & " is not in the list."
Else
MsgBox "Score of " & student & ": " & score
End If
End Sub
If you leave the input box empty or click Cancel, the code simply ends.
VLOOKUP for All the Values in a Column (Data on Another Sheet)
The loop code above works for five values only. If your list can grow, it’s better to find the last row and loop till there. In the following code, the lookup values start from cell D3 on the active sheet, and the data is on a sheet named “Data”.
Sub vlookup_all_rows()
Dim lastRow As Long
Dim i As Long
Dim result As Variant
lastRow = Cells(Rows.Count, "D").End(xlUp).Row
For i = 3 To lastRow
result = Application.VLookup(Cells(i, "D").Value, Worksheets("Data").Range("A:B"), 2, False)
If IsError(result) Then
Cells(i, "E").Value = "Not found"
Else
Cells(i, "E").Value = result
End If
Next i
End Sub
- Last row: Cells(Rows.Count, “D”).End(xlUp).Row finds the last filled cell in column D, so you don’t need to change the code when you add more values.
- Another sheet: Worksheets(“Data”).Range(“A:B”) tells VLOOKUP to look in the data on the “Data” sheet. Change the name to match your sheet. For data in another open workbook, add the workbook name in front, like Workbooks(“Scores.xlsx”).Worksheets(“Data”).Range(“A:B”).
- Long instead of Integer: An Integer variable can only go up to 32,767, so use Long for row numbers.
Enter the VLOOKUP Formula in Cells with VBA
There’s one more way. Instead of getting the result with VBA, you can enter the VLOOKUP formula itself in all the cells at once. Excel adjusts the relative reference (D3) for each row, just like when you drag a formula down.
Sub vlookup_formula()
With Range("E3:E7")
.Formula = "=IFERROR(VLOOKUP(D3,$A:$B,2,FALSE),""Not found"")"
' Optional: keep only the results and remove the formulas
.Value = .Value
End With
End Sub
Inside the code, the formula is a text string, so the quotes around “Not found” are doubled. Always write the formula with commas in the Formula property, even if your Excel uses semicolons; VBA expects the English (US) formula syntax there. The formula that this code enters in cell E3 is:
=IFERROR(VLOOKUP(D3,$A:$B,2,FALSE),"Not found")
If you want the formulas to stay in the cells and update when the data changes, remove the .Value = .Value line.
Use XLOOKUP in VBA
If you use Excel for Microsoft 365, Excel 2024 or Excel 2021, you can also use XLOOKUP through the WorksheetFunction property. It takes the lookup range and the result range separately, so the result column can be on the left side of the lookup column as well, and its fourth argument (if_not_found) gives a value to return when there’s no match.
Sub xlookup_score()
Range("F3").Value = WorksheetFunction.XLookup(Range("E3").Value, Range("A:A"), Range("B:B"), "Not found")
End Sub
XLOOKUP is not available in Excel 2019 and earlier versions, so this code gives an error there. If other people with older versions will run your macro, stick with VLOOKUP.
Common Mistakes with VLOOKUP in VBA
- Numbers stored as text: If the IDs in your data are numbers and the lookup value is text (or the other way round), VLOOKUP can’t find a match. An InputBox always returns text, so convert it with CLng or Val when you look up numbers.
- Extra spaces: A name with a trailing space (“John “) doesn’t match “John”. Use Trim on the lookup value, or clean the data first.
- Wrong column index number: The column number counts from the first column of the table range, not from column A of the sheet. A number bigger than the columns in the range gives an error.
- Approximate match by mistake: If you leave out the fourth argument, VLOOKUP uses TRUE (approximate match) and can return a wrong value from unsorted data. Use FALSE for an exact match.
- Code in the wrong place: Put the macro in a standard module (Insert > Module in the Visual Basic Editor), and make sure the right sheet is active when you run it, as Range(“A:B”) without a sheet name refers to the active sheet.
Which Method to Use
- WorksheetFunction.VLookup: When you’re sure the value is in the data, or you want the code to stop when it isn’t.
- Application.VLookup: When some values may be missing and you want to handle that yourself (show “Not found” and carry on).
- VLOOKUP formula with the Formula property: When you want to fill a whole column at once, or keep live formulas in the cells.
- WorksheetFunction.XLookup: When everyone who runs the macro has Microsoft 365, Excel 2024 or Excel 2021, and you need to look to the left or want a built-in “not found” value.
Frequently Asked Questions
Why do I get run-time error 1004 with VLOOKUP in VBA?
WorksheetFunction.VLookup gives run-time error 1004 when it can’t find the lookup value or when the column index number is outside the table. Use Application.VLookup and test the result with IsError, as shown above, so the code doesn’t stop.
Is VLOOKUP in VBA case-sensitive?
No. VLOOKUP treats uppercase and lowercase text as the same, so “john” and “John” find the same row, in VBA and in the worksheet.
Can I use wildcards with VLOOKUP in VBA?
Yes. With FALSE in the fourth argument, you can use an asterisk (*) for any number of characters and a question mark (?) for one character in a text lookup value. For example, “Jo*” as the lookup value finds the first name that starts with “Jo”. To look for an actual asterisk or question mark, put a tilde (~) before it.
Can VLOOKUP in VBA return a value from a column on the left?
No. VLOOKUP always looks in the first column of the table and returns a value from a column to its right. To look to the left, use WorksheetFunction.XLookup (Microsoft 365, Excel 2024 and Excel 2021), or WorksheetFunction.Index with WorksheetFunction.Match in older versions.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.