In VBA, a lookup searches a range or table for a value and returns a related value. For a straightforward exact match, use Excel’s WorksheetFunction.XLookup or WorksheetFunction.VLookup; for more control, loop through cells or use Range.Find. The right method depends on your Excel version, data layout, and how you want missing matches handled.
What a lookup means in VBA
VBA is Excel’s programming language for automating tasks. A lookup in a macro follows the same basic idea as a worksheet lookup: find a key, such as a product code or employee ID, then retrieve a value from the same row or a corresponding location. For example, a macro might find the price associated with an item code before creating an invoice.
VBA does not have one universal “lookup” statement. You choose among Excel worksheet functions called from VBA, Excel’s range-searching methods, or your own search logic. A worksheet formula is usually easiest when the result belongs in a cell. A VBA lookup is useful when the result controls a larger procedure or should be stored in a variable.
Use XLOOKUP for a flexible exact match
In Microsoft 365 for Windows, WorksheetFunction.XLookup provides a direct way to retrieve a match. XLOOKUP is available in Microsoft 365 and Excel 2021 or later. This example searches product codes in column A on a sheet named Products and returns the corresponding description from column B.
Sub FindProductDescription()
Dim lookupValue As String
Dim result As Variant
' Look for this product code in Products column A.
lookupValue = "P104"
' Return the matching description from column B.
On Error Resume Next
result = Application.WorksheetFunction.XLookup( _
lookupValue, Worksheets("Products").Range("A2:A100"), _
Worksheets("Products").Range("B2:B100"))
If Err.Number <> 0 Then
Err.Clear
result = "Not found"
End If
On Error GoTo 0
MsgBox result
End Sub
With the code and ranges shown, the macro returns the description on the row where P104 appears, or displays “Not found” if there is no match. The error handling matters because WorksheetFunction.XLookup raises a VBA error if it cannot find a match and no fallback value is provided. Another option is to call Application.XLookup, which returns an Excel error value that your code can test with IsError.
Use On Error Resume Next narrowly, as above, and restore normal error handling immediately with On Error GoTo 0. Leaving it active across a larger procedure can hide unrelated problems.
Use VLookup when the return column is to the right
WorksheetFunction.VLookup is familiar and works in older Excel versions. It searches the first column of a table range and returns a value from a numbered column to its right. For an exact match, its fourth argument must be False.
Sub FindPriceWithVLookup()
Dim result As Variant
' Search column A and return the third column of A:C.
On Error Resume Next
result = Application.WorksheetFunction.VLookup( _
"P104", Worksheets("Products").Range("A2:C100"), 3, False)
If Err.Number <> 0 Then
Err.Clear
result = "Not found"
End If
On Error GoTo 0
MsgBox result
End Sub
Here, the lookup value is searched in column A, and the third column of the specified range—column C—is returned. VLOOKUP cannot return a value from a column to the left of the search column. It can also break if someone inserts or rearranges columns and the hard-coded return-column number no longer points to the intended field. XLOOKUP avoids those particular limitations by accepting separate lookup and return ranges.
Use Range.Find to locate a cell
Choose Range.Find when you need the matching cell itself, its address, or other information from its row. It also works in older Excel versions. Specify important search settings explicitly: Excel remembers some Find settings from previous searches, including searches made through the Find dialog.
Sub FindProductCell()
Dim foundCell As Range
' Search the product-code range for an exact value.
Set foundCell = Worksheets("Products").Range("A2:A100").Find( _
What:="P104", After:=Worksheets("Products").Range("A100"), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If foundCell Is Nothing Then
MsgBox "Product code not found"
Else
MsgBox "Found on row " & foundCell.Row
End If
End Sub
When using Find, always check whether the returned object is Nothing before reading its row or value. Set LookAt:=xlWhole for an exact cell match; xlPart finds text containing the search term. If searching all cells where a value may occur more than once, use FindNext and stop when the search returns to the first found address.
Choose the method and handle data carefully
Use the method that matches the shape of the task:
- XLOOKUP: A convenient exact or approximate lookup with separate search and return ranges. Best when the workbook uses Excel 2021 or Microsoft 365.
- VLOOKUP: A broadly compatible choice when the key is in the leftmost column of the search table and the result is to its right.
- Range.Find: A good fit when you need the matching cell, its row, or a search through a range rather than simply a returned value.
- A loop: Useful for custom rules, multiple matches, or processing many records. For large datasets, repeated cell-by-cell access can be slow; reading a range into a VBA array can reduce worksheet interaction.
Lookups can fail even when the values appear identical. Check for leading or trailing spaces, different data types, and inconsistent formatting. A number stored as text may not match a numeric value. Case is usually not significant in XLOOKUP and VLOOKUP, while a case-sensitive search requires custom logic. Also confirm that the lookup range covers all relevant records and that duplicate keys are handled intentionally: a standard lookup returns one match, not every matching row.
Run macros safely
Before running a macro that changes workbook data, save a separate copy. VBA actions cannot be undone with Ctrl+Z, and a lookup macro may feed results into later steps that alter cells. In Microsoft 365 for Windows, open the Visual Basic Editor with Alt+F11; on a Mac, the common shortcut is Option+F11 (some keyboards require Fn as well). The Developer tab may need to be enabled to access the editor or macro commands. Excel for the web can open workbooks containing VBA but cannot create, run, or edit VBA macros; use the desktop app.
Frequently Asked Questions
Can VBA use an Excel lookup formula?
Yes. You can call functions such as WorksheetFunction.VLookup or WorksheetFunction.XLookup from VBA. The function returns a value to your code, rather than automatically writing a worksheet formula into a cell.
How do I return a blank or message when no match exists?
With WorksheetFunction.XLookup, you can provide its optional fourth argument as a not-found value. Alternatively, handle the error in VBA, as in the example. With Range.Find, test whether the result is Nothing.
Is XLOOKUP better than VLOOKUP in VBA?
Often, when available: XLOOKUP uses separate lookup and return ranges and can return values from either side of the key. VLOOKUP remains useful for older Excel versions and familiar table layouts.
Can a VBA lookup return more than one match?
A single XLOOKUP or VLOOKUP call returns one result. To collect multiple matches, loop through the data, use repeated FindNext searches, or use an appropriate dynamic-array worksheet function and process its results.
Why does my VBA lookup say a value is missing?
Check for extra spaces, text-versus-number differences, a search range that excludes the record, and an unintended approximate-match setting. For VLOOKUP, use False as the fourth argument when an exact match is required.