VBA & Macro Problems

Search a Closed Excel Workbook with VBA

Jason MillerJason Miller Nov 22, 2026 869 views

Question details

The user needs to look up values in an Excel workbook that is normally closed and write matching results to another workbook.

How to Search a Closed Excel Workbook with VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A VBA macro reads account numbers from column A and searches a separate customer-list workbook for corresponding values.
Observed behavior
The proposed ExecuteExcel4Macro VLOOKUP approach is not a reliable way to perform the closed-workbook lookup.
Before you start

Confirm the source workbook path, worksheet name, and lookup columns. The example assumes account numbers are in column A, results are in column C, and both sheets have headers in row 1.

Solution 1Recommended

Open the source workbook in VBA and match its values

Use this approach when the macro can temporarily open the source workbook to perform exact-match lookups.

The macro searches the populated portion of source column A and writes the corresponding column C value to column M of the active sheet. It closes the source workbook only if the macro opened it.

1
Open the VBA editor

In Excel, open the destination workbook, select the worksheet containing the account numbers, and press Alt+F11. In the VBA editor, choose Insert > Module.

2
Add the lookup macro

Paste the following code into the module. Change sourcePath and Sheet1 to match your source file and worksheet: Public Sub LookupCustomerValues() Dim sourcePath As String Dim srcWb As Workbook, w As Workbook Dim srcWs As Worksheet, destWs As Worksheet Dim lastSrc As Long, lastDest As Long, r As Long Dim matchRow As Variant Dim openedHere As Boolean On Error GoTo ErrorHandler sourcePath = "C:\JunkTest\Customer List.xls" Set destWs = ActiveSheet For Each w In Application.Workbooks If StrComp(w.FullName, sourcePath, vbTextCompare) = 0 Then Set srcWb = w Exit For End If Next w If srcWb Is Nothing Then Set srcWb = Workbooks.Open(Filename:=sourcePath, ReadOnly:=True, UpdateLinks:=0) openedHere = True End If Set srcWs = srcWb.Worksheets("Sheet1") lastSrc = srcWs.Cells(srcWs.Rows.Count, "A").End(xlUp).Row lastDest = destWs.Cells(destWs.Rows.Count, "A").End(xlUp).Row If lastSrc < 2 Then Err.Raise vbObjectError + 1, , "No source data found below the header." For r = 2 To lastDest If Len(destWs.Cells(r, "A").Value) > 0 Then matchRow = Application.Match(destWs.Cells(r, "A").Value, srcWs.Range("A2:A" & lastSrc), 0) If IsError(matchRow) Then destWs.Cells(r, "M").Value = "Not found" Else destWs.Cells(r, "M").Value = srcWs.Cells(CLng(matchRow) + 1, "C").Value End If End If Next r If openedHere Then srcWb.Close SaveChanges:=False Exit Sub ErrorHandler: If openedHere Then srcWb.Close SaveChanges:=False MsgBox "Lookup stopped: " & Err.Description, vbExclamation End Sub

3
Run the macro

Return to the destination worksheet, select Developer > Macros, choose LookupCustomerValues, and click Run. Check column M for results or "Not found".

Open the source workbook in VBA and match its values
Check account number formats: Exact matches can fail if one sheet stores an account number as text and the other stores it as a number. Make the formats consistent before running the macro.
Free Microsoft Office alternative

Work with Excel files in WPS Spreadsheet

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use worksheet lookup formulas while moving between office suites.

Use spreadsheet lookup formulas to find matching values in worksheet dataOpen and work with Microsoft Excel XLS and XLSX filesNavigate a familiar interface for a smoother transition from Microsoft OfficeAccess a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Can VLOOKUP retrieve data without opening the source workbook?

Excel can use supported external references to retrieve data from a closed workbook, but the proposed ExecuteExcel4Macro loop is not a reliable general-purpose lookup method. Opening the source in VBA or importing its data provides a clearer workflow.

Will the VBA macro close a workbook I already have open?

No. The example checks open workbooks by full path and closes the source only when the macro opened it.

Why does the macro return "Not found" for an existing account?

Check that the source path and Sheet1 are correct, and compare the account number formats in both sheets. A number stored as text may not match the same value stored as a number.

How do I update data imported with Power Query?

In the destination workbook, select Data > Refresh All. The query retrieves updated source data, and the worksheet lookup recalculates against the imported values.