VBA & Macro Problems

How to Use a Variable Workbook Name in Excel XLOOKUP VBA

Chanuka GeekiyanageChanuka Geekiyanage Nov 2, 2026 869 views

Question details

The user wants to replace a hard-coded external workbook name inside an XLOOKUP VBA macro with a dynamic variable that pulls the workbook name from a specific cell on an Excel worksheet.

How to Use a Variable Workbook Name in Excel XLOOKUP VBA
Product
Excel
Device & OS
not provided
Scenario
Writing a VBA macro using WorksheetFunction.XLookup where the target reference resides in an external workbook, and the name of that workbook must be flexible rather than hard-coded.
Observed behavior
The user needs to correctly concatenate string variables to build the complete external workbook and range reference so it can be passed into the XLOOKUP function.
Before you start

Verify whether the target workbook's references are named ranges, table column headers, or ordinary standard column headers, as this changes how the string must be formatted.

Solution 1Recommended

Dynamically Build External Range References as Strings

Create dynamic range references by concatenating the workbook name retrieved from a worksheet cell with the named ranges or column headers, then pass these strings to your XLOOKUP function.

To make an XLOOKUP reference dynamic in VBA, you can extract the target workbook's name directly from a cell on your active sheet. By concatenating this variable with the syntax required for external references, you avoid having to manually update the macro whenever the external file changes.

1
Declare string variables

In your VBA editor, declare variables to hold the lookup array and the return array strings. For example: Dim strWbtitle As String and Dim strWbpath As String.

2
Retrieve the workbook name from a cell

Use the ThisWorkbook.Worksheets method to get the value of the cell containing the target workbook name. For instance, ThisWorkbook.Worksheets("Sheet1").Range("F1").Value.

3
Concatenate strings to form the reference

Construct the full reference by adding brackets around the workbook name and appending the column headers. Example: strWbtitle = "[" & ThisWorkbook.Worksheets("Sheet1").Range("F1").Value & "!TX_DB_LOC_title]".

4
Pass the references to XLOOKUP

Use the built strings in your XLOOKUP function. Example: test = WorksheetFunction.XLookup("path", strWbtitle, strWbpath). (Note: Depending on your exact implementation, you may need to use Evaluate() to convert the string into a valid Range object).

Dynamically Build External Range References as Strings
Table Headers vs. Standard Ranges: Ensure you know exactly what TX_DB_LOC_title and TX_DB_LOC_path are. If they are ordinary column headers in a standard range rather than named ranges, you may need to specify the sheet name within the string as well.
Advanced Spreadsheet Automation

Write and Execute VBA Macros in WPS Spreadsheet

WPS Spreadsheet fully supports VBA macros, allowing you to use dynamic XLOOKUP functions, automate complex data references, and seamlessly manage variable workbook names.

  1. 1. Open your macro-enabled workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data.
  2. 2. Access the VBA Editor: Navigate to the 'Tools' or 'Developer' tab on the top ribbon and click on 'Visual Basic' or 'Macro' to open the VBA environment.
  3. 3. Insert the dynamic XLOOKUP code: Double-click your module in the Project Explorer and paste your concatenated string references and XLOOKUP macro code.
  4. 4. Run your macro: Press F5 or click 'Run' to execute the code and automatically fetch data from the dynamically referenced external workbook.
Full support for standard VBA code and macros100% compatible with Microsoft Excel formats (.xlsx, .xlsm)Free built-in data analysis functions including XLOOKUPLightweight software with fast macro execution speeds
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a Type Mismatch error when passing string variables to XLOOKUP in VBA?

The WorksheetFunction.XLookup method expects Range objects, not String objects. If you pass raw strings, you may encounter a Type Mismatch. To fix this, wrap your concatenated string in the Evaluate() function or use the Range() object to convert the string reference into a valid Range before passing it to XLOOKUP.

Can I use a variable worksheet name alongside a variable workbook name?

Yes, you can dynamically concatenate both the workbook name and the worksheet name. Ensure the string follows the standard Excel external reference format: enclose the workbook name in square brackets [], followed directly by the worksheet name, and then an exclamation mark ! before the cell range.

Does the external workbook need to be open for XLOOKUP to work in VBA?

When using WorksheetFunction.XLookup with external references in VBA, the source workbook typically needs to be open in the background to evaluate the range properly and prevent #REF! errors.