To open a workbook with VBA, call Workbooks.Open and give it the file’s full path. For example, Set wb = Workbooks.Open(Filename:="C:\Reports\Monthly.xlsx") opens that file in desktop Excel and stores a reference to it in wb.
What Workbooks.Open Does
Workbooks.Open is an Excel VBA method that opens an existing workbook in the current Excel application. It returns a Workbook object, which you can use to read sheets, change cells, save updates, or close the file. The method does not create a new workbook; use Workbooks.Add when you need a new one.
A path can point to a local drive, a mapped network drive, or a location Excel can access. The filename extension should match the actual file type, such as .xlsx, .xlsm, or .xlsb. In a macro, assign the returned object to a variable rather than assuming that the newly opened workbook will always be the active workbook.
The following basic procedure uses a declared workbook variable and a full Windows path:
Sub OpenWorkbook()
Dim wb As Workbook
Dim filePath As String
' Set this to the workbook's actual location.
filePath = "C:\Reports\Monthly.xlsx"
Set wb = Workbooks.Open(Filename:=filePath)
End Sub
To try it, open the Visual Basic Editor with Alt+F11 on Windows, choose Insert > Module, and paste in the procedure. Change the path before running it. On Mac, use Option+F11 (on some keyboards, also hold Fn) to open the editor; VBA code is for desktop Excel, not Excel for the web.
Opening a Workbook Step by Step
A reliable macro starts with a known path and handles the returned workbook explicitly. Before running any macro that may change files, save a separate copy of the workbook you are working with. VBA actions cannot be reversed with Ctrl+Z, so a backup is the practical way to protect against unwanted edits.
- Confirm the workbook exists at the intended location and note its exact filename and extension.
- In Excel, open the Visual Basic Editor, insert a standard module, and add a procedure.
- Declare a
Workbookvariable and aStringvariable for the path. - Set the path, then call
Workbooks.Openwith that path. - Run the macro and confirm that the expected workbook appears. If the macro will edit it, save only after checking the changes.
On Windows, backslashes separate folders in a typical local path, as in C:\Reports\Monthly.xlsx. On Mac, a path may use a Mac-style location, and access to files can depend on macOS privacy permissions and where the file is stored. The safest approach is to use a path that Excel on that computer can already open. A Windows drive letter will not identify a Mac file location.
Choosing Open Options
Workbooks.Open has optional arguments that help control how a file is opened. The most commonly useful choices are whether to update links and whether to open in read-only mode. Here is an example that avoids updating external links and prevents changes from being saved over the source file:
Sub OpenWorkbookReadOnly()
Dim wb As Workbook
Dim filePath As String
' Set this to the workbook's actual location.
filePath = "C:\Reports\Monthly.xlsx"
Set wb = Workbooks.Open(Filename:=filePath, UpdateLinks:=0, ReadOnly:=True)
End Sub
UpdateLinks:=0 tells Excel not to update external references when opening the workbook. It does not remove those references; their displayed values may remain based on previously stored results. If you want Excel to update links, omit the argument and respond to Excel’s prompts as appropriate, or explicitly choose the update behavior you need. ReadOnly:=True opens the file without allowing the macro to save changes back to that source workbook.
Other optional arguments can supply a password for a protected workbook or choose how to handle text files. Avoid hard-coding passwords in a macro that may be shared or inspected. Also distinguish a workbook’s open password from worksheet protection: they are different protections, and an open password is required before Excel can read the workbook contents.
Letting the User Select a File
If the file location varies, a file picker is more useful than editing a path inside the code. This example allows the user to select one Excel workbook and exits without opening anything if the dialog is cancelled:
Sub OpenSelectedWorkbook()
Dim picker As FileDialog
Dim selectedPath As String
Dim wb As Workbook
Set picker = Application.FileDialog(msoFileDialogFilePicker)
With picker
.Title = "Select a workbook to open"
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Excel workbooks", "*.xlsx; *.xlsm; *.xlsb; *.xls"
If .Show <> -1 Then Exit Sub
selectedPath = .SelectedItems(1)
End With
Set wb = Workbooks.Open(Filename:=selectedPath)
End Sub
The picker’s filter is a convenience for the selection window, not a guarantee that a chosen file is valid or accessible. The user may still encounter a damaged file, a permission problem, or a password prompt. For a macro intended to open only one known file, a fixed path can be simpler; for a reusable tool used by different people, asking them to browse is often less error-prone.
Handling Errors and Missing Files
A hard-coded path can become invalid when someone renames a folder, moves the workbook, disconnects a network drive, or runs the macro on a different computer. Without error handling, VBA stops at the failed statement and displays a runtime error. A short handler can present a useful message instead:
Sub OpenWorkbookWithErrorHandling()
Dim wb As Workbook
Dim filePath As String
' Set this to the workbook's actual location.
filePath = "C:\Reports\Monthly.xlsx"
On Error GoTo OpenFailed
Set wb = Workbooks.Open(Filename:=filePath, UpdateLinks:=0)
Exit Sub
OpenFailed:
MsgBox "Excel could not open the workbook: " & Err.Description, vbExclamation
End Sub
This handler reports the error but does not fix the underlying cause. Check the spelling, extension, folder permissions, network connection, and whether another process is using the file. A password prompt or security warning may also require a person to respond; an error handler is not a way to bypass those protections.
For larger automation, consider checking that the file exists before attempting to open it and reporting the full path in the message. Be careful with paths on shared drives: a drive letter mapped for one Windows user may not be mapped for another. A network location that works in File Explorer under one account may also be unavailable to Excel running under a different account or security context.
Working With the Workbook After It Opens
Once the method returns a workbook object, refer to that object directly. This is more dependable than using ActiveWorkbook, which means whichever workbook currently has focus and can change if the macro opens another file or the user clicks elsewhere. For example, the following macro reads a value from the first worksheet of the opened file:
Sub ReadOpenedWorkbook()
Dim wb As Workbook
Dim filePath As String
Dim reportValue As Variant
' Set this to the workbook's actual location.
filePath = "C:\Reports\Monthly.xlsx"
Set wb = Workbooks.Open(Filename:=filePath, ReadOnly:=True)
reportValue = wb.Worksheets(1).Range("A1").Value
MsgBox "The value in A1 is: " & reportValue
End Sub
The object variable wb points to the workbook that was opened, and Worksheets(1) means its first worksheet tab. If you know the sheet name, referencing it by name can make the intent clearer, but the name must match exactly. A workbook may also contain chart sheets, so Worksheets(1) specifically selects the first worksheet rather than the first sheet of any kind.
Opening a workbook does not automatically mean you should save or close it. If you make changes, decide deliberately whether to save them; saving a read-only workbook back to its source is not appropriate. Closing with SaveChanges:=False discards unsaved changes, so do not use that option casually.
Already-Open Files and Duplicate Workbooks
A macro may be run more than once, or the user may already have opened the target workbook. Before opening it again, you can look through the workbooks in the current Excel instance and reuse a workbook whose full path matches. The comparison below is a simple exact-path check:
Sub OpenUnlessAlreadyOpen()
Dim filePath As String
Dim item As Workbook
Dim target As Workbook
' Set this to the workbook's actual location.
filePath = "C:\Reports\Monthly.xlsx"
For Each item In Application.Workbooks
If StrComp(item.FullName, filePath, vbTextCompare) = 0 Then
Set target = item
Exit For
End If
Next item
If target Is Nothing Then
Set target = Workbooks.Open(Filename:=filePath)
End If
End Sub
This helps when the stored full path and supplied path are written the same way, ignoring letter case. It is not a universal path-normalization routine: a mapped drive and a network share can point to the same file but have different path strings. The check also sees workbooks in the current Excel instance, not necessarily files open in a separate Excel process.
Saving the Macro and Using It Safely
If you place the code in the workbook that contains it, save that file as an Excel Macro-Enabled Workbook (.xlsm) to retain the VBA project. An ordinary .xlsx file does not store VBA macros; saving a macro-enabled file as .xlsx removes the macro project. You can also store reusable code in another macro-enabled workbook or an add-in, depending on how broadly it needs to be available.
Excel may block macros from files downloaded from the internet or show a security notice. Only enable macros when you trust the file and understand what its code will do. On Windows, macro settings are available through File > Options > Trust Center > Trust Center Settings > Macro Settings, although organization policies can control or disable these choices. Do not lower security globally just to run an unfamiliar macro.
VBA runs in desktop Excel for Windows and Mac, with some platform differences in paths, dialogs, and supported features. Excel for the web can open and edit many workbooks, but it cannot create, run, or edit VBA macros. If a workflow must run in a browser, consider Office Scripts or another supported automation option instead.
Frequently Asked Questions
What is the VBA command to open an Excel file?
Use Workbooks.Open, typically with a full filename: Set wb = Workbooks.Open(Filename:=filePath). Declare wb as a Workbook variable and set filePath to a location Excel can access.
How do I open a workbook read-only with VBA?
Pass the named argument ReadOnly:=True to Workbooks.Open. For example, use Set wb = Workbooks.Open(Filename:=filePath, ReadOnly:=True). This opens the source without treating it as an editable copy to save over the original.
Can VBA open a workbook without updating links?
Yes. Add UpdateLinks:=0 to the call to prevent Excel from updating external links as it opens the workbook. This leaves the links in place; it does not refresh their values.
Can I run an Excel VBA macro in Excel for the web?
No. Excel for the web does not run or edit VBA macros. Open the file in desktop Excel to use VBA, or investigate Office Scripts for browser-based automation.
Why does Workbooks.Open say the file cannot be found?
Check that the path includes the correct folder, filename, and extension, and that the current user can access that location. Network drives, renamed folders, and differences between Windows and Mac paths are common causes.
Do I need to use ActiveWorkbook after opening a file?
No. Assign the result of Workbooks.Open to a Workbook variable and refer to that variable. It identifies the intended workbook even if focus changes to another open file.
Related
- Excel Paste and Remove Duplicates: Steps and Safety
- Excel Text to Columns: Split Pasted Data Into Fields
- Excel Text to Columns: Can You Make It Automatic?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Range Filter: How to Use AutoFilterTo filter a worksheet range with VBA in Excel for Microsoft 365 on Windows, call the range’s AutoFilter…
- Excel Macro Error? Common Causes and FixesAn Excel macro error usually means VBA is blocked, the code cannot find an expected workbook, worksheet, or…
- Excel VBA Active Worksheet Reference ExplainedIn Excel VBA, use ActiveSheet to refer to the sheet currently active in the active workbook. For a…
- Excel VBA: Save a Workbook as a New File with SaveAsTo save an Excel workbook as a new file with VBA, use the workbook’s SaveAs method and supply…
- Free Macro Recorder for Windows 11: Best OptionsYes. Windows 11 users can record and replay desktop actions for free with Microsoft Power Automate for desktop,…
- Word VBA Mail Merge: How to Automate ItWord VBA can automate a mail merge by telling a prepared Word merge document to create a new…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.