Excel VBA: How to Reference Another Workbook
In this article
To reference another workbook in Excel VBA, open the target workbook, assign it to an object variable with Workbooks("Name.xlsx") or Workbooks.Open("C:\Path\Name.xlsx"), then qualify every range or sheet reference with that object. The shortest working pattern is: Dim wb As Workbook: Set wb = Workbooks.Open("C:\Data\Report.xlsx"), then wb.Worksheets("Sheet1").Range("A1").Value = 100. Always close the workbook when done and set the object to Nothing to release the reference.
What You'll Need
- Microsoft Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- The full file path of the workbook you want to reference, including the extension (.xlsx, .xlsm, .xlsb).
- Basic familiarity with the VBA editor (Alt+F11) and inserting a standard module.
- Both workbooks saved in a trusted location, or macro security set to allow the code to run.
Step-by-Step
- Open the VBA editor. Press Alt+F11 in Excel. In the Project Explorer, right-click your workbook and choose Insert > Module. All code below goes in that module.
- Declare object variables. At the top of the procedure, declare a Workbook variable for the source and, if needed, one for the destination:
Dim wbSource As WorkbookandDim wbDest As Workbook. Using variables avoids repeating long names and makes the code easier to read. - Point to the destination workbook. Set
Set wbDest = ThisWorkbookif the code lives in the workbook you are writing to.ThisWorkbookalways refers to the file containing the macro, which is safer thanActiveWorkbookbecause the active workbook can change. - Open or assign the source workbook. If the file is already open, use
Set wbSource = Workbooks("Report.xlsx"). If it is closed, useSet wbSource = Workbooks.Open("C:\Data\Report.xlsx"). The Open method returns a Workbook object, so it must be assigned withSet. - Reference a worksheet inside the source. Qualify the sheet with the workbook object:
wbSource.Worksheets("Sheet1"). You can also use the sheet index,wbSource.Worksheets(1), or the code name,wbSource.Sheet1, if you know it. Avoid unqualified references likeWorksheets("Sheet1")because they default to the active workbook. - Read or write a range. To copy a value:
wbDest.Worksheets("Summary").Range("A1").Value = wbSource.Worksheets("Sheet1").Range("B2").Value. To copy a whole range with formatting:wbSource.Worksheets("Sheet1").Range("A1:C10").Copy Destination:=wbDest.Worksheets("Summary").Range("A1"). - Close the source workbook when finished. Use
wbSource.Close SaveChanges:=Falseif you opened it and made no changes, orSaveChanges:=Trueif you need to keep edits. Closing a workbook you did not open is risky; only close files your code opened. - Release the object references. End with
Set wbSource = NothingandSet wbDest = Nothing. This frees memory and prevents stale references if the code runs again. - Add error handling. Wrap the procedure with
On Error GoTo Cleanupand a cleanup label that closes the workbook and clears the objects. This prevents a locked file if the code fails halfway through. - Run and test. Press F5 or use Run > Run Sub. Check that the destination cells contain the expected values and that the source workbook is closed afterward.
Common Mistakes
- Forgetting
Set. WritingwbSource = Workbooks.Open(...)withoutSetcauses a runtime error because objects requireSet. - Using
ActiveWorkbookinstead ofThisWorkbook. The active workbook changes when you open another file, so references can silently point at the wrong book. - Unqualified sheet references.
Worksheets("Sheet1")without a workbook prefix refers to the active workbook, not the one you intended. - Hard-coding a path that does not exist. A missing file raises "Run-time error 1004". Verify the path and extension.
- Leaving the source workbook open. The next run may fail because the file is locked, or you may accidentally close a file the user had open.
- Assuming the sheet name. Sheet tab names can differ from the code name; check the actual tab text.
Tips
- Use
ThisWorkbook.Pathto build relative paths, e.g.ThisWorkbook.Path & "\Report.xlsx", so the macro works when the folder is moved. - Check whether the file is already open before calling
Workbooks.Open; opening an already-open file returns the existing object but can confuse users. - Prefer
WorksheetsoverSheetswhen you only need worksheets, sinceSheetsalso includes chart sheets. - Use
Option Explicitat the top of the module to catch undeclared variables. - Store the full path in a constant or a cell so users can change it without editing code.
Troubleshooting
If you get "Subscript out of range" (error 9), the workbook or worksheet name does not match exactly, including spaces and capitalization. If you get "Run-time error 1004", the path is wrong, the file is open in another instance, or the sheet is protected. If the macro runs but writes to the wrong file, check that every range reference is prefixed with the correct workbook variable. If the file stays locked after running, make sure the cleanup code closes the workbook and sets objects to Nothing. If the code cannot find the file on a network drive, test with a local path first to rule out permissions.
Frequently Asked Questions
Can I reference a workbook without opening it?
No. VBA must open a workbook to read or write its cells. You can open it invisibly with Workbooks.Open and close it immediately afterward, but it must be opened.
What is the difference between ThisWorkbook and ActiveWorkbook?
ThisWorkbook is the file containing the running macro. ActiveWorkbook is whichever workbook currently has focus, which can change as code runs.
How do I reference a closed workbook's data without macros?
Use external links in formulas, such as ='C:\Data\[Report.xlsx]Sheet1'!A1. VBA is not required for that.
Why does my macro fail when the workbook is already open?
Opening an already-open file can return the existing object, but if it was opened by another user or instance, it may be read-only or locked. Check the file state before opening.
Can I reference a workbook by its window name?
Yes, Workbooks("Report.xlsx") uses the file name as the key. Window captions can include "Read-Only" or other text, so prefer the file name.
Do I need to close the workbook I referenced?
Only if your code opened it. Closing a workbook the user opened manually can lose their unsaved work.