How to Fix XLOOKUP Text If-Not-Found Errors in Excel VBA
Question details
The user needs to return a text string or an empty string as the if-not-found value in an XLOOKUP formula applied via VBA, without triggering syntax or type-mismatch errors.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to insert an XLOOKUP formula into a worksheet that includes a text-based fallback value when no match is found.
- Observed behavior
- Numeric fallback values work correctly, but attempting to output text like a hyphen or an empty string causes VBA syntax errors or type-mismatch errors.
Ensure your VBA editor is open and verify that the target cells and lookup ranges referenced in your macro are correct and exist in the active workbook.
Escape Quotation Marks in Your VBA String
Properly double the quotation marks within your VBA code so the resulting worksheet formula recognizes the text fallback correctly.
When passing a formula from VBA to a worksheet cell, any quotation marks intended for the final Excel formula must be escaped. Because VBA strings are enclosed in quotes, you must type two consecutive double quotes inside the VBA string to represent a single quotation mark in the cell formula.
Press Alt + F11 to open the Visual Basic Editor, and locate the macro module where you are writing the XLOOKUP function.
Find the line of code assigning the formula to a cell, typically formatted as ActiveCell.Formula = "..."
To output a specific text character like a hyphen when no match is found, double the internal quotes. Update your code to: ActiveCell.Formula = "=XLOOKUP(BV1, BT:BT, BU:BU, ""-"")"
To leave the cell blank when no match is found, use four consecutive quotation marks for the fallback argument: ActiveCell.Formula = "=XLOOKUP(BV1, BT:BT, BU:BU, """")"

Easily Manage Formulas and Macros with WPS Office
WPS Spreadsheet provides excellent compatibility with Microsoft Excel macros, VBA scripts, and modern formulas like XLOOKUP. You can write, edit, and troubleshoot your macros in a familiar, high-performance environment.
- 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and open WPS Spreadsheet.
- 2. Enable Developer Tools: Navigate to the Developer tab in the top ribbon and click on 'Macros' or 'Visual Basic' to access the built-in VBA editor.
- 3. Write and Execute Code: Paste your correctly escaped XLOOKUP macro code into a module and run it to instantly populate your worksheet cells.

Frequently Asked Questions
Why do I get a 'Type Mismatch' error in VBA when using XLOOKUP?
A Type Mismatch or syntax error usually occurs when VBA tries to evaluate an unescaped string inside your formula as a variable or executable code. Ensuring all text strings inside ActiveCell.Formula are enclosed in doubled quotes prevents the compiler from misinterpreting them.
Can I use VBA variables as the if-not-found value in XLOOKUP?
Yes. You can concatenate a VBA variable into your formula string using an ampersand (&). For example: ActiveCell.Formula = "=XLOOKUP(A1, B:B, C:C, """ & myVariable & """")".
What if my version of the software doesn't support XLOOKUP?
XLOOKUP is a modern function available in recent versions of spreadsheet software. If your environment does not support it, you can achieve the same result using a combination of IFERROR and VLOOKUP, such as: ActiveCell.Formula = "=IFERROR(VLOOKUP(A1, B:C, 2, FALSE), ""-"")".




