VBA Open Excel File From Path: Code and Common Fixes

VBA & macros · Updated November 20, 2026

To open an Excel workbook from a known path in VBA, call Workbooks.Open and pass the full path as its Filename argument. For example, Workbooks.Open Filename:="C:\Reports\Quarterly.xlsx" opens that workbook in desktop Excel if the path is valid and the file is accessible.

Open a workbook from a fixed path

This complete example checks whether the file can be found, opens it, and reports an error if Excel cannot open it. Change the sample path to the location and name of your workbook. In the VBA editor, insert a standard module and place the procedure there.

Option Explicit

Sub OpenWorkbookFromPath()
    Dim filePath As String
    Dim openedBook As Workbook

    ' Replace this example with the workbook's full path.
    filePath = "C:\Reports\Quarterly.xlsx"

    If Len(Dir$(filePath)) = 0 Then
        MsgBox "File not found: " & filePath, vbExclamation
        Exit Sub
    End If

    On Error GoTo OpenFailed
    Set openedBook = Workbooks.Open(Filename:=filePath, UpdateLinks:=0)
    MsgBox "Opened: " & openedBook.Name, vbInformation
    Exit Sub

OpenFailed:
    MsgBox "Excel could not open the workbook: " & Err.Description, vbExclamation
End Sub

Before running a macro, save a separate copy of any workbook the macro may change. Macro actions cannot generally be reversed with Ctrl+Z, and an open operation may be followed by other code that edits or saves data. This example suppresses automatic updating of external links; it does not make the workbook read-only.

What the path and Workbooks.Open mean

A path identifies both the folder and the file. On Windows, a typical full path begins with a drive letter, such as C:\Reports\Quarterly.xlsx. A network share may use a UNC path such as \\ServerName\TeamFiles\Quarterly.xlsx. In VBA, backslashes are ordinary characters; they do not need to be doubled in a string.

Workbooks is Excel’s collection of open workbooks. Its Open method opens a workbook and returns a Workbook object, which the sample stores in openedBook. Keeping that reference lets later statements address the file just opened without guessing which workbook happens to be active. The Filename argument is the location to open. Named arguments make the code easier to read than relying on a long list of positional arguments.

The path must include the file’s extension, such as .xlsx, .xlsm, or .xls. The extension should match the actual file. A path to a folder alone is not a workbook path, and a filename without its folder will work only if Excel’s current working directory happens to be the right one.

Use a file picker when the path varies

If the user chooses a different workbook each time, show Excel’s file picker instead of hard-coding a location. This example limits the picker to Excel workbook types and exits cleanly if the user presses Cancel.

Option Explicit

Sub ChooseAndOpenWorkbook()
    Dim picker As FileDialog
    Dim selectedPath As String
    Dim openedBook As Workbook

    Set picker = Application.FileDialog(msoFileDialogFilePicker)
    With picker
        .Title = "Select an Excel workbook"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Excel workbooks", "*.xlsx;*.xlsm;*.xlsb;*.xls"
        .InitialFileName = "C:\Reports\"

        If .Show <> -1 Then Exit Sub
        selectedPath = .SelectedItems(1)
    End With

    On Error GoTo OpenFailed
    Set openedBook = Workbooks.Open(Filename:=selectedPath, UpdateLinks:=0)
    MsgBox "Opened: " & openedBook.Name, vbInformation
    Exit Sub

OpenFailed:
    MsgBox "Excel could not open the selected file: " & Err.Description, vbExclamation
End Sub

The filter is a convenience for selecting a file, not a guarantee that the selected file is valid or safe. The selected path is captured before calling Workbooks.Open; the workbook is then assigned to a variable so later code can refer to it explicitly.

Choose useful Open options

Workbooks.Open accepts optional arguments that control how Excel opens a file. Add only the options your task needs, because choices such as read-only mode affect what the macro can do afterward.

  • ReadOnly:=True requests a read-only opening. It is useful when the macro only needs to inspect or copy data and should not edit the source. It does not prevent a user from making a separate copy or guarantee that the file is protected from every other change.
  • UpdateLinks:=0 tells Excel not to update external workbook links when opening. This avoids automatic link updates, but linked values may remain at their saved values. If fresh linked data is required, choose a deliberate link-update policy instead.
  • Notify:=True can ask Excel to notify the user when a file is not immediately available for writing. This is relevant to shared or locked files, rather than a general substitute for handling errors.
  • Password and WriteResPassword can supply passwords for protected files. Do not put passwords directly into shared or widely distributed code; use an appropriate secure process for credentials.

If a workbook must be edited, omit ReadOnly:=True or set it to False, and check that the user has permission to write to the file. Excel may still open it read-only if another user has an exclusive lock or the folder is not writable.

Refer to the right workbook after opening

A frequent VBA mistake is assuming that ThisWorkbook means the workbook just opened. It means the workbook containing the running VBA code. If the macro is stored in a personal macro workbook or an add-in, ThisWorkbook refers to that container, not the data file. By contrast, ActiveWorkbook refers to whichever workbook is active at that moment, and another window or event can change that.

For dependable code, use the object returned by Workbooks.Open, as in openedBook.Worksheets("Summary").Range("A1").Value. This makes the target explicit. Confirm that the named worksheet exists; otherwise, that reference raises an error even though the file opened successfully. If you intentionally want to switch the user to the workbook, use openedBook.Activate, but activation is not required to read or write through the object variable.

Relative paths and changing folders

A fixed absolute path is usually easiest to understand and maintain. Relative paths depend on a base location, and Excel VBA code should not assume that the current directory is the workbook’s folder. If a data file is stored beside the workbook containing the macro, build the path from that workbook’s location:

Option Explicit

Sub OpenSiblingWorkbook()
    Dim filePath As String
    Dim openedBook As Workbook

    ' ThisWorkbook is the file that contains this macro.
    filePath = ThisWorkbook.Path & Application.PathSeparator & "Data.xlsx"

    If Len(Dir$(filePath)) = 0 Then
        MsgBox "File not found: " & filePath, vbExclamation
        Exit Sub
    End If

    Set openedBook = Workbooks.Open(Filename:=filePath, UpdateLinks:=0)
End Sub

Application.PathSeparator supplies the path separator used by the running Excel application, which is helpful when code needs to work across Windows and Mac. A workbook that has never been saved may have an empty ThisWorkbook.Path, so save it first or handle that situation before constructing a sibling-file path. Files synced with cloud services can also have different local paths on different computers; a hard-coded user-specific folder may fail elsewhere.

Common errors and practical checks

When opening fails, first inspect the exact value passed as Filename. A misspelled folder, missing extension, moved file, or extra character is often the cause. The Dir$ check in the examples catches many missing-file cases, but it is not a complete permission or file-integrity test. A file can exist and still be unavailable to Excel.

  • Permission denied or read-only: Check folder permissions, whether another user has locked the workbook, and whether the file is marked read-only. Network connections and cloud-sync status can also affect access.
  • Wrong format or damaged file: Confirm the extension matches the file and try opening it manually in Excel. A renamed non-Excel file is not converted just because its name ends in .xlsx.
  • Password prompt or protected view: A password-protected workbook may require the appropriate password argument. Files from the internet or email may also open in Protected View or be affected by Trust Center settings; do not disable security protections just to bypass a warning.
  • External links or prompts: Excel can display prompts related to links, read-only recommendations, or file formats. Specify suitable options and test with representative files rather than suppressing every alert indiscriminately.
  • Unexpected workbook content: Check the returned Workbook object’s name and path before changing data. A workbook with the expected name may already be open from another folder, or the selected file may not be the intended one.

Use error handling around the open operation when a macro should recover gracefully. During development, inspect Err.Number and Err.Description in the error handler rather than hiding the failure. Avoid a broad On Error Resume Next around the whole procedure: it can make later code continue after the file never opened, producing more confusing errors.

Windows, Mac, and Excel for the web

The examples target desktop Excel for Microsoft 365 on Windows. VBA is also supported in desktop Excel for Mac, but the selected file’s path and permissions may differ; using a file picker or Application.PathSeparator can reduce operating-system assumptions. In the VBA editor, Windows commonly uses Alt+F11 to open the editor. On Mac, Option+F11 is commonly used, and some keyboards require Fn as well. Menu names and shortcut behavior can vary with keyboard settings and Excel builds.

Excel for the web does not run VBA macros. A workbook can be stored or viewed there, but run this VBA in desktop Excel. On Mac, confirm that the target folder is available to Excel and that macOS privacy permissions allow access; a valid-looking Windows drive-letter path will not identify a Mac file.

Frequently Asked Questions

What is the VBA command to open an Excel file?
Use Workbooks.Open, for example Workbooks.Open Filename:="C:\Reports\Quarterly.xlsx". Replace the example with the full path to the workbook.

How do I open a workbook without updating links?
Set UpdateLinks:=0 in the Workbooks.Open call. That prevents Excel from updating external workbook links during opening; it does not refresh linked data.

How can I open a workbook read-only with VBA?
Use Workbooks.Open(Filename:=filePath, ReadOnly:=True), where filePath contains the full path. Read-only mode is appropriate for inspection, but it limits editing the opened source workbook.

How do I open a file from the same folder as my macro workbook?
Build the path with ThisWorkbook.Path & Application.PathSeparator & "Data.xlsx". Save the macro workbook first so ThisWorkbook.Path identifies a folder.

Can VBA open an Excel file in Excel for the web?
No. VBA macros run in desktop Excel, not Excel for the web. Open the workbook in the Windows or Mac desktop application to run the macro.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.