Excel VBA XLOOKUP: How to Use It in Macros
In this article
You can call XLOOKUP from Excel VBA by using the WorksheetFunction.XLOOKUP method, which takes the same arguments as the worksheet function and returns the matched value (or the if_not_found value). The shortest working form is WorksheetFunction.XLOOKUP(lookup_value, lookup_array, return_array). This guide walks through a reliable macro pattern, the optional arguments, error handling, and the mistakes that cause run-time errors.
What You'll Need
- Excel with XLOOKUP available (Microsoft 365 and Excel 2021 or later; XLOOKUP is not in Excel 2019 or earlier). If you open a workbook that uses XLOOKUP in an older version, the formula returns #NAME?.
- A workbook with at least one lookup table: a column of keys and one or more columns of values to return.
- Macro security set to allow macros, and the Developer tab visible (File > Options > Customize Ribbon > check Developer). Menu labels can differ slightly by version and region.
- Basic familiarity with the VBA Editor (Alt+F11) and the Immediate Window (Ctrl+G).
Step-by-Step
- Set up your data. Put your lookup keys in one column (for example A2:A100 on a sheet named Data) and the values you want back in another column on the same sheet (for example C2:C100). XLOOKUP does not require the return column to be to the right of the lookup column, which is one reason it is easier than VLOOKUP.
- Open the VBA Editor with Alt+F11, then insert a module: Insert > Module. All the code below goes in that module.
- Write a minimal macro. Declare variables, read the lookup value from a cell, and call the function:
Run it with F5. The matched value lands in F2.Sub SimpleXLookup() Dim result As Variant result = WorksheetFunction.XLOOKUP( _ Range("E2").Value, _ Worksheets("Data").Range("A2:A100"), _ Worksheets("Data").Range("C2:C100")) Range("F2").Value = result End Sub - Add the optional arguments in order. The full signature is
XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode]). Supply them positionally, not by name. For example, to return "Not found" instead of an error and to require an exact match:result = WorksheetFunction.XLOOKUP( _ Range("E2").Value, _ Worksheets("Data").Range("A2:A100"), _ Worksheets("Data").Range("C2:C100"), _ "Not found", 0, 1)match_mode0 is exact match, -1 exact or next smaller, 1 exact or next larger, 2 wildcard.search_mode1 searches first to last, -1 last to first, 2 and -2 are binary searches that require sorted data. - Return multiple columns if needed. Point
return_arrayat a multi-column range such as C2:D100 and XLOOKUP returns an array. In VBA, assign it to a Variant and either write it to a matching-size range or read individual items. Writing a two-column array to a single cell only shows the first value, so size the destination range correctly. - Handle the not-found case in code. Even with
if_not_foundsupplied, other problems (a mismatched array size, a closed workbook) raise errors. Wrap the call:On Error Resume Next result = WorksheetFunction.XLOOKUP( _ Range("E2").Value, _ Worksheets("Data").Range("A2:A100"), _ Worksheets("Data").Range("C2:C100"), _ "Not found") If Err.Number <> 0 Then result = "Lookup failed: " & Err.Description Err.Clear End If On Error GoTo 0 - Loop over many lookup values. For a column of keys, loop from the first to the last used row and write results to the adjacent column. Reading ranges into arrays first is much faster than touching cells one at a time on large sheets.
- Test edge cases. Try a key that exists, one that does not, a blank key, a numeric key stored as text, and a case-variant key. XLOOKUP is not case-sensitive by default, so "abc" and "ABC" match the same row.
Common Mistakes
- Using
Application.XLOOKUPinstead ofWorksheetFunction.XLOOKUP. The Application object does not expose it; you get a run-time error 438 (object does not support this property or method). - Passing named arguments. VBA's WorksheetFunction methods do not accept the argument names shown in the formula bar. Pass values in order or you get a compile or run-time error.
- Mismatched array sizes.
lookup_arrayandreturn_arraymust have the same number of rows (or columns). A one-row offset produces #VALUE! or an error in VBA. - Forgetting that errors surface as VBA errors. A worksheet #N/A becomes a trappable run-time error in VBA, not a cell value, unless you supply
if_not_found. - Assuming XLOOKUP exists everywhere. Workbooks shared with Excel 2019 users will show #NAME? on their machines. For shared files, consider INDEX/MATCH, which works in all versions.
- Binary search modes on unsorted data. Using 2 or -2 without sorting the lookup column returns wrong matches silently.
Tips
- Qualify every range with its worksheet. Unqualified
Range("A2:A100")refers to the active sheet and breaks when the user switches sheets. - Store lookup and return ranges in variables once, then reuse them in loops to keep code readable and fast.
- Use
match_mode2 with wildcards (* and ?) when you need partial matches, for example finding a product code that starts with a prefix. - If you need the position rather than the value, XLOOKUP alone will not give it; combine it with MATCH or return a helper column of row numbers.
- Keep a small test sheet with known keys so you can rerun the macro after edits and confirm results instantly.
Troubleshooting
If the macro fails, work from simplest to most involved. First, check that the lookup value actually exists in the lookup column, including leading or trailing spaces; TRIM the value before passing it. Second, confirm both ranges are the same size and on the sheet you named. Third, verify the workbook is open and the sheet name is spelled exactly, including spaces (wrap names with spaces in quotes inside Worksheets("...")). Fourth, check the Excel version on the machine running the macro; if XLOOKUP is missing, the call errors every time. Fifth, step through the code with F8 and inspect values in the Immediate Window using ? result to see what the function returned before it was written to a cell. If you get error 1004, it usually means a range reference is invalid or a sheet is protected; unprotect the sheet or write to an unprotected cell. If results are wrong rather than erroring, suspect search_mode 2 or -2 on unsorted data, or a lookup column containing numbers stored as text. Convert with VALUE or TEXT so both sides use the same type.
Frequently Asked Questions
Can I use XLOOKUP in VBA in Excel 2019?
No. XLOOKUP was introduced after Excel 2019, so the WorksheetFunction method is not available there. Use INDEX/MATCH or VLOOKUP in VBA for older versions.
Why do I get run-time error 438 with XLOOKUP?
You likely called it on the wrong object, such as Application.XLOOKUP. Use WorksheetFunction.XLOOKUP instead.
How do I return a blank instead of an error when nothing matches?
Pass an empty string as the fourth argument: WorksheetFunction.XLOOKUP(key, lookRange, returnRange, "").
Does XLOOKUP in VBA support wildcards?
Yes, when you set match_mode to 2. Use * for any number of characters and ? for a single character.
Can XLOOKUP return more than one column?
Yes. Point return_array at a multi-column range; the result is an array, which you can write to a same-sized destination range.
Is XLOOKUP case-sensitive in VBA?
No. It matches regardless of case, the same as on the worksheet. For case-sensitive matching you need a different approach, such as EXACT combined with array formulas.