Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA XLOOKUP Multiple Criteria: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To use XLOOKUP with multiple criteria in Excel VBA, you build a single lookup value by concatenating the criteria and a single lookup array by concatenating the corresponding columns, then call WorksheetFunction.XLookup (or Application.XLookup for older builds). This works because XLOOKUP accepts arrays, so you can match on a combined key instead of a single cell. You need Excel 365 or Excel 2021+ with XLOOKUP available, and the Developer tab enabled (File > Options > Customize Ribbon > check Developer).

What You'll Need

  • Excel 365 or Excel 2021 or later (XLOOKUP is not in Excel 2019 or earlier).
  • Developer tab enabled: File > Options > Customize Ribbon > tick Developer in the right pane.
  • A worksheet with at least two criteria columns and one return column, for example columns A (Region), B (Product), C (Sales).
  • Basic familiarity with the VBA editor (Alt+F11) and running a macro (Alt+F8).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11, then Insert > Module to create a blank module. Menu labels can differ slightly by Excel version and region, but Insert > Module is consistent.
  2. Write the macro skeleton. Declare variables for the worksheet, the lookup values and the result:
    Sub MultiXLookup()
        Dim ws As Worksheet
        Dim crit1 As String, crit2 As String
        Dim result As Variant
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        crit1 = ws.Range("E2").Value   ' first criterion
        crit2 = ws.Range("F2").Value   ' second criterion
    End Sub
    Adjust sheet name and cell references to match your data.
  3. Build the concatenated lookup value. Combine the criteria with a delimiter that cannot appear in your data, such as a pipe character:
    Dim lookupVal As String
    lookupVal = crit1 & "|" & crit2
    The delimiter prevents false matches (e.g., "AB"+"C" matching "A"+"BC").
  4. Build the concatenated lookup array. Use Evaluate to concatenate the two columns on the fly:
    Dim lookupArr As Variant
    lookupArr = ws.Evaluate("A2:A100&""|""&B2:B100")
    Replace A2:A100 and B2:B100 with your actual ranges. Keep the ranges the same size.
  5. Call XLOOKUP. Pass the lookup value, the array and the return range:
    result = Application.WorksheetFunction.XLookup( _
        lookupVal, lookupArr, ws.Range("C2:C100"), "Not found")
    The fourth argument is the if_not_found value, which avoids a #N/A error when no match exists.
  6. Write the result to a cell. For example:
    ws.Range("G2").Value = result
    Then close the Sub with End Sub.
  7. Run the macro. Press Alt+F8, select MultiXLookup and click Run. Check that G2 shows the expected value. If it shows "Not found", verify that the criteria cells contain exactly the same text (no extra spaces) as the source columns.

Common Mistakes

  • Mismatched range sizes. The lookup array and return array must have the same number of rows. A2:A100 with C2:C99 will fail or return wrong results.
  • Using a delimiter that appears in the data. If your text contains a pipe, choose a different separator such as CHAR(1) or a tilde.
  • Forgetting the if_not_found argument. Without it, a missing match raises a runtime error 1004 in VBA rather than returning a blank.
  • Using WorksheetFunction.XLookup on Excel 2019 or earlier. The function does not exist there; the macro will fail to compile or run.
  • Hard-coding sheet names. If the sheet is renamed, the macro breaks. Use the sheet's CodeName or a variable.
  • Leaving trailing spaces in criteria. Use Trim() on crit1 and crit2 before concatenating.

Tips

  • For three or more criteria, chain more columns: lookupVal = c1 & "|" & c2 & "|" & c3 and extend the Evaluate string accordingly.
  • Wrap the XLookup call in error handling (On Error Resume Next) only if you also clear the error afterwards; otherwise use the if_not_found argument.
  • Use Application.XLookup instead of WorksheetFunction.XLookup if you want to avoid the runtime error and get an error value back instead.
  • For large ranges, limit the Evaluate ranges to the used range (e.g., ws.UsedRange) to keep performance reasonable.
  • Test with a known match and a known non-match before running on the full dataset.

Troubleshooting

  • Runtime error 1004: Usually a missing match without if_not_found, or a range size mismatch. Add the fourth argument and check row counts.
  • Result is #VALUE!: The concatenated array contains an error or the ranges include blank rows that break the Evaluate string. Restrict to populated rows.
  • Macro returns the wrong row: The delimiter appears inside your data, causing a false concatenation match. Switch to CHAR(1).
  • Compile error: Sub or Function not defined: XLOOKUP is not available in your Excel version. Use INDEX/MATCH with two criteria as an alternative, or upgrade to Microsoft 365.
  • Nothing happens when running: Check that the module is in the correct workbook and that macros are enabled (File > Options > Trust Center > Macro Settings).

Frequently Asked Questions

Can I use XLOOKUP with multiple criteria without VBA?

Yes. In a worksheet formula, concatenate criteria and ranges directly, e.g. =XLOOKUP(E2&F2, A2:A100&B2:B100, C2:C100, "Not found"). The VBA approach is for automating the lookup inside a macro.

Does XLOOKUP work in Excel 2019?

No. XLOOKUP is available only in Excel 365 and Excel 2021 or later. In older versions, use INDEX/MATCH with a concatenated key instead.

How do I handle more than two criteria?

Add more columns to both the lookup value and the lookup array, separated by the same delimiter. Keep all ranges the same size.

Why does my macro return "Not found" when the data exists?

Check for leading or trailing spaces in the criteria cells and in the source columns. Use Trim() on both sides, or clean the data first.

Can I return multiple columns with one XLOOKUP call?

Yes. Set the return array to multiple columns, e.g. C2:E100, and XLOOKUP returns the whole row. Assign it to a Variant and write it to a range.

Is there a performance difference between Evaluate and a helper column?

Evaluate recalculates the concatenation each time the macro runs. For very large datasets, a helper column with the concatenated key is faster and easier to debug.