VBA & Macro Problems

Fix Excel VBA VLOOKUP References to Another Open Workbook

Khadija KhanKhadija Khan Nov 25, 2026 869 views

Question details

Write a VBA VLOOKUP formula in one workbook using a table range in another open workbook without run-time error 1004.

Fix Excel VBA VLOOKUP References to Another Open Workbook
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro assigns a VLOOKUP formula to a destination cell while the lookup table is in a separate open workbook.
Observed behavior
Excel reports run-time error 1004 when the macro assigns the formula.
Before you start

Open both workbooks in the same Excel instance and check their exact workbook and worksheet names. Confirm that the destination cell is writable and that the lookup table contains the requested return column.

Solution 1Recommended

Build a Qualified VLOOKUP Formula With Escaped Quotes

Use explicit workbook and worksheet references and correctly escape the empty-string result before assigning the formula.

In a VBA string, use four consecutive quotation marks to produce the two quotation marks of an empty text value in the Excel formula. External range addresses include the workbook and worksheet names, avoiding ambiguous references.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. Open the module containing your macro, or choose Insert > Module.

2
Build and assign the formula

Replace the workbook names, sheet names, ranges, and return-column number in this example as needed. Then paste it into the module: Sub TestVLookup() Dim wb1 As Workbook, wb2 As Workbook Dim lookupCell As Range, tableRange As Range Dim myFormula As String Set wb1 = Workbooks("Invoice Reconciliation Template.xlsm") Set wb2 = Workbooks("Invoice.xls") Set lookupCell = wb1.Worksheets("Totals").Range("B2") Set tableRange = wb2.Worksheets("Current").Range("B2:BZ500") myFormula = "=IF(ISERROR(VLOOKUP(@lookup,@table,16,FALSE)),"""",VLOOKUP(@lookup,@table,16,FALSE))" myFormula = Replace(myFormula, "@lookup", lookupCell.Address(0, 1, External:=True)) myFormula = Replace(myFormula, "@table", tableRange.Address(1, 1, External:=True)) wb1.Worksheets("Totals").Range("F2").Formula = myFormula End Sub

3
Run and check the result

Run TestVLookup from the VBA editor, then select Totals!F2 in the destination workbook and inspect the formula bar. If the error persists, check the workbook and sheet names, the table range, and whether F2 is protected.

Build a Qualified VLOOKUP Formula With Escaped Quotes
Errors appear as blank results: The IF and ISERROR combination returns an empty string for any VLOOKUP error, not just a missing match. The number 16 selects the 16th column within the lookup table.
Free Microsoft Office alternative

Work With Lookup Spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easier move between office suites. It does not fix errors in Microsoft Excel VBA.

Use spreadsheet lookup formulas to work with tabular data.Open and edit Microsoft Excel formats such as XLS and XLSX.Navigate a familiar, easy-to-use spreadsheet interface.Move existing Office files into a lightweight productivity suite.
QA img-9

Frequently Asked Questions

Why are four quotation marks needed in the VBA formula string?

VBA uses doubled quotation marks to represent one literal quotation mark inside a string. Four consecutive quotation marks therefore produce the two quotation marks in the formula's empty text value, "".

Do both workbooks need to be open for this macro?

Yes. The example uses the Workbooks collection to obtain both files by name, so open them in the same Excel instance before running the macro.

What does the 16 in VLOOKUP refer to?

It is the return-column index counted from the left edge of the table range. For B2:BZ500, column B is index 1, so index 16 returns a value from column Q.

Why does the formula return a blank instead of an error?

The IF(ISERROR(...),"",...) expression replaces any VLOOKUP error with an empty string. Check the lookup value and table range if you expected a match.