logo
search
VBA & Macro Problems

How to Fix XLOOKUP Text If-Not-Found Errors in Excel VBA

Olivia MillerOlivia Miller Oct 10, 2026 869 views

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.

How to Fix XLOOKUP Text If-Not-Found Errors in VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic Editor, and locate the macro module where you are writing the XLOOKUP function.

2
Locate the Formula Assignment

Find the line of code assigning the formula to a cell, typically formatted as ActiveCell.Formula = "..."

3
Format for a Text String (e.g., Hyphen)

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, ""-"")"

4
Format for an Empty String

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, """")"

Escape Quotation Marks in Your VBA String
Syntax Tip: The outermost quotes define the entire VBA string, while the doubled inner quotes are interpreted as literal text quotes in the final worksheet formula.
Seamless VBA & Macro Support

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. 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and open WPS Spreadsheet.
  2. 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. 3. Write and Execute Code: Paste your correctly escaped XLOOKUP macro code into a module and run it to instantly populate your worksheet cells.
Fully compatible with Microsoft Excel .xlsm and .xlsx file formats.Built-in developer tools and VBA editor for seamless macro troubleshooting.Native support for advanced spreadsheet functions including XLOOKUP and XMATCH.Lightweight software with a highly familiar user interface for immediate productivity.
microsoft office alternative - wps office

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), ""-"")".