Excel VBA: Reference Another Workbook (Step-by-Step)
In this article
To reference another workbook in Excel VBA, open that workbook in code (or use an already-open one), then qualify every range or sheet reference with the workbook object. The shortest working pattern is: Set wb = Workbooks.Open("C:\Path\File.xlsx"), then wb.Worksheets("Sheet1").Range("A1").Value. If the other workbook is already open, use Workbooks("File.xlsx") instead of opening it again. Always close what you open and release object variables when done.
What You'll Need
- Excel with macros enabled (an .xlsm file for the workbook that holds the code).
- The exact file path of the workbook you want to reference, including the extension.
- Permission to read (and, if you will write, modify) that file.
- Basic familiarity with the VBA editor: press Alt+F11 to open it, and Ctrl+G to show the Immediate window for quick tests.
Menu labels and dialog wording can differ slightly by Excel version, device and region; the code itself is the same across desktop Excel for Windows and Mac. The steps below are written for desktop Excel.
Step-by-Step
- Decide whether the other workbook will be open or closed. If it is already open, you can reference it by name. If it is closed, you must open it in code first. Opening a closed workbook is slower but more reliable because you control exactly which file is used.
- Declare object variables. At the top of your procedure, declare a Workbook variable and, if useful, a Worksheet variable:
Dim wb As WorkbookandDim ws As Worksheet. This keeps your code readable and avoids repeated long references. - Open the workbook (if closed). Use the full path:
Set wb = Workbooks.Open("C:\Reports\Data.xlsx"). On Mac, use the Mac path style or theMacScript/AppleScriptTaskapproach if the path contains spaces or special characters; the simplest reliable method is to use the path shown in Finder's Get Info window. If the workbook is already open, replace this line withSet wb = Workbooks("Data.xlsx"). - Reference a specific sheet and range. Qualify fully:
Set ws = wb.Worksheets("Sheet1"), then read or write a value:ws.Range("A1").Value = "Hello"orMsgBox ws.Range("A1").Value. Avoid unqualified references likeRange("A1")because they act on the active workbook, which may not be the one you intend. - Handle the case where the sheet name might differ. If you are not certain of the sheet name, use the index:
wb.Worksheets(1)refers to the first sheet in that workbook. This is safer than guessing a name that may have been renamed. - Write data back and save. If you change values in the other workbook, save it explicitly:
wb.Save(orwb.SaveAsif you want a copy). Do not rely on Excel prompting the user. - Close the workbook and release objects. End with
wb.Close SaveChanges:=False(orTrueif you saved changes) andSet ws = Nothing,Set wb = Nothing. This prevents orphaned Excel processes and file locks. - Test with a small procedure. Run a short macro that opens the file, reads one cell, shows it in a message box, and closes the file. Confirm the value is what you expect before building the full routine.
Common Mistakes
- Using unqualified Range or Cells.
Range("A1")refers to the active sheet of the active workbook. Always prefix with the workbook and worksheet object. - Forgetting to open a closed workbook. Referencing
Workbooks("Data.xlsx")when the file is not open raises a subscript out of range error. Open it first or checkWorkbookscollection membership. - Hard-coding a path that changes. A path that works on your machine may not exist on a colleague's. Store the folder in a cell or a constant and build the path with
ThisWorkbook.Path & "\Data.xlsx"when the files sit together. - Leaving the workbook open. Every
Workbooks.Openshould have a matchingClose. Otherwise the file stays locked and the next run fails. - Assuming the active workbook is the one with the code. Use
ThisWorkbookto mean the file containing the macro, andActiveWorkbookonly when you truly mean whatever is in front.
Tips
- Use
Application.ScreenUpdating = Falsebefore opening andTrueafter closing to speed up routines that touch large files. - Set
Application.DisplayAlerts = Falseonly around the close step if you want to suppress save prompts, and turn it back on immediately afterward. - Prefer
ThisWorkbook.Pathover absolute paths when the two files travel together. - Use
Dir()to check a file exists before opening it, which gives a clearer error message than a failed Open call. - Name your variables clearly:
wbSource,wbTarget,wsData.
Troubleshooting
- Subscript out of range (Error 9): the workbook name or sheet name is wrong, or the file is not open. Check the exact name including extension and case, and confirm the sheet exists.
- File not found (Error 53 or 1004): the path is wrong or the file has moved. Print the path with
Debug.Printto the Immediate window and compare it to the real location. - Permission denied: the file is open in another Excel instance, marked read-only, or stored in a protected folder. Close other instances and check file properties.
- Macro runs on the wrong workbook: you used an unqualified reference. Replace
Range(...)withwb.Worksheets("Sheet1").Range(...). - Excel process stays in Task Manager: an object was not released. Add
Set wb = NothingandSet ws = Nothingat the end, and ensure every Open has a Close. - If that did not work: step through the code with F8 to see exactly which line fails, and check the Locals window for the current values of
wbandws.
Frequently Asked Questions
Can I reference a workbook without opening it?
Not for normal range reads and writes. You can pull data from a closed workbook with ADO or by linking, but the standard object model requires the workbook to be open. Open it in code, use it, then close it.
How do I reference a workbook by its full path instead of its name?
Use Workbooks.Open with the full path to get a Workbook object, then use that object. The Workbooks collection indexes open workbooks by name, not by path, so the object variable is the reliable route.
What is the difference between ThisWorkbook and ActiveWorkbook?
ThisWorkbook is the file that contains the running macro. ActiveWorkbook is whatever workbook currently has focus, which may be a different file. Use ThisWorkbook when you mean your own file.
Why does my macro work once and then fail the second time?
The most common cause is that the first run left the other workbook open or left a reference pointing at a closed file. Add explicit Close and Set ... = Nothing lines at the end of the routine.
Can I reference a workbook on a network drive or SharePoint?
Yes, if the path is reachable and you have permission. Use the full UNC path (for example \\server\share\file.xlsx) rather than a mapped drive letter, because mapped letters can differ between users.
How do I avoid hard-coding the file name?
Store the name in a cell or a constant, or use ThisWorkbook.Path to build a relative path. That way the macro keeps working when files are moved together.