VBA Code to Open an Excel File from a Path

VBA & macros · Updated November 20, 2026

Use VBA’s Workbooks.Open method to open a workbook by its full file path. In Excel for Microsoft 365 on Windows, assign the path to a string, check that the file exists, and pass it to the method; the example below also reports an error if Excel cannot open the file.

Basic VBA code to open a workbook by path

Change the sample path to the location and name of your file, including its extension. This version avoids updating external links automatically and shows a message if the file is missing or cannot be opened.

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

    ' Set this to the workbook's full path.
    filePath = "C:\Reports\Quarterly.xlsx"
    ' For a workbook beside this macro workbook, use this instead:
    ' filePath = ThisWorkbook.Path & Application.PathSeparator & "Quarterly.xlsx"

    ' Check that the file exists before trying to open it.
    If Len(Dir(filePath)) = 0 Then
        MsgBox "File not found: " & filePath, vbExclamation
        Exit Sub
    End If

    On Error GoTo OpenError
    ' Open the workbook without updating linked workbooks.
    Set targetWorkbook = Workbooks.Open(Filename:=filePath, UpdateLinks:=0)
    Exit Sub

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

The macro opens the file in the current Excel application. It does not copy the workbook into the file containing the macro, and it does not automatically make that workbook the active one in every situation. The returned workbook is assigned to targetWorkbook so later code can refer to it directly.

What the file path should look like

A full path identifies both the folder and the file. On Windows, a typical path starts with a drive letter, followed by a colon and backslash. Include the exact file name and extension: an Excel workbook might end in .xlsx, .xlsm, or .xls. The path is text in VBA, so it needs straight quotation marks around it.

  • Local Windows file: C:\Reports\Quarterly.xlsx
  • File in a nested folder: C:\Users\Ava\Documents\Finance\Budget.xlsx
  • Network share: \\ServerName\Shared\Reports\Quarterly.xlsx

Spaces in folder or file names are fine when the full path is inside the quoted string. Do not add extra quotation marks around the path itself, and check that the extension matches the actual file. A misspelling, an outdated folder name, or a mapped network drive that is unavailable to the current user can all prevent the open operation.

The optional ThisWorkbook.Path approach in the sample builds a path relative to the workbook that contains the macro. It is useful when you distribute a macro workbook and its data file together. Save the macro workbook first: before it has been saved, its path is empty. Application.PathSeparator supplies the separator Excel uses on the current platform, which is more adaptable than typing a separator into that expression.

How Workbooks.Open works

Workbooks is Excel’s collection of open workbooks, and Open tells Excel to load another workbook into that collection. The Filename argument supplies the file location. The method returns a Workbook object, which is why the example uses Set when assigning its result to an object variable. You can then use that variable to read sheets or cells without relying on whichever workbook happens to be active.

The example sets UpdateLinks to zero. This tells Excel not to update external references as part of opening the file. If you need linked data refreshed, decide deliberately whether links should be updated; updating may retrieve values from other workbooks and could produce prompts or delays. If you omit the argument, Excel uses its normal behavior and may ask how to handle links, depending on settings and the workbook.

Opening a workbook is different from importing a worksheet. The entire workbook opens as a separate workbook window or document in the same Excel session. If your goal is to bring data into the current workbook, you will need additional VBA to copy a sheet, range, or values after opening the source.

Where to put and run the macro

VBA runs in the desktop version of Excel, not in Excel for the web. In Excel for Microsoft 365 on Windows, open the workbook that will contain the macro and press Alt+F11 to open the Visual Basic Editor. On Mac, use the Developer tab’s Visual Basic command if available; keyboard shortcuts can vary with the keyboard and macOS settings. In the editor, choose Insert > Module, paste the procedure into the module, and edit the path.

  1. Save a copy of the workbook before running any macro. VBA actions cannot be undone with Ctrl+Z.
  2. Save the macro-containing workbook as an Excel Macro-Enabled Workbook (.xlsm) if you need to retain the code.
  3. Return to Excel and run the procedure with Alt+F8 on Windows, select OpenWorkbookFromPath, and choose Run. On Mac, use the Macro command on the Developer tab or the available macro shortcut.

If the Developer tab is hidden in Windows Excel, enable it through File > Options > Customize Ribbon and select Developer. A workbook saved as .xlsx does not store VBA macros; Excel may remove the code if you save a macro-containing file in that format. The source workbook you are opening can still be an ordinary .xlsx file.

Opening a workbook read-only or changing link behavior

By default, Excel generally opens a file so it can be edited, subject to its permissions and any prompts. If the macro should inspect a source file without saving changes to it, use the ReadOnly option. This is a useful safeguard for reporting or review tasks, but it is not a substitute for making a backup when the macro also changes other workbooks.

Sub OpenWorkbookReadOnly()
    Dim filePath As String
    Dim sourceWorkbook As Workbook

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

    ' Open a read-only copy and do not update external links.
    Set sourceWorkbook = Workbooks.Open( _
        Filename:=filePath, _
        UpdateLinks:=0, _
        ReadOnly:=True)
End Sub

Read-only mode prevents saving changes back to the original file through that opened instance, although users can still use Save As to create another file. For a password-protected workbook, Excel may request a password or return an error if the required password is not supplied. Avoid putting passwords directly into a macro that will be shared or stored where others can read its code.

Common errors and practical fixes

A “file not found” message usually means the path does not point to an accessible file. Copy the file’s location from File Explorer, then verify that the filename and extension are included. If the path is on a network, confirm the share is available and that the user running Excel has permission. A mapped drive letter may not be available in a different Windows login or elevated session; a network share path can be more reliable in that situation.

  • Excel reports a format or extension problem: Confirm the file is a valid workbook and that its extension matches its format. Renaming a non-Excel file to .xlsx does not convert it.
  • The macro opens the wrong copy: Check for duplicate filenames in different folders and use a complete path rather than a relative one.
  • A link or password prompt appears: Review the workbook’s external links and protection requirements. The example’s link setting prevents link updates, but it cannot bypass a password.
  • The macro cannot access a cloud-synced file: Confirm the file is downloaded and available at the local path being used. A browser sharing link is not necessarily a valid local file path for Workbooks.Open.
  • The workbook is already open: Excel may activate the existing instance rather than create another independent copy. If you need to avoid opening the same file twice, check the open workbooks before calling Open.

The error handler in the first example catches an error raised during the open operation and displays Excel’s description. It is intentionally simple. For automated jobs, you may want to log the path and error to a worksheet or text file rather than rely on a message box, which pauses execution until someone responds.

Security and safe macro use

Only enable macros in workbooks from sources you trust. VBA can do more than open files: it can edit or delete data, run other operations, or interact with files outside Excel. Keep macro security enabled, inspect code received from others, and do not lower security settings globally just to run one procedure. An “Enable Content” prompt is not proof that a workbook is safe.

Also consider what the macro will do after opening its target. The sample opens a workbook but does not change its cells. If you add code that edits or saves data, test it on a separate copy first and verify which workbook the code is addressing. Ctrl+Z does not reverse VBA changes, so a saved backup is the practical recovery option.

Alternatives to a fixed file path

A hard-coded path is simple and appropriate when the file remains in the same location. For a workbook that moves between computers, a path based on ThisWorkbook.Path can keep related files together. Another option is to let the user choose a file through Excel’s file picker; that avoids editing the VBA string each time, though the macro needs extra code to display the picker and handle a canceled selection.

If you only need to open a file occasionally, Excel’s File > Open command may be simpler than a macro. If you need repeatable automated work, VBA can open the file and then process its contents. For data importing and transformation, Power Query may be a better fit because it can connect to supported file sources and refresh queries without writing a workbook-opening macro. Excel for the web does not run VBA, so use desktop Excel for this procedure or choose a web-compatible workflow.

Frequently Asked Questions

What is the VBA command to open an Excel file?
Use the Workbooks.Open method with the file’s full path as its Filename argument. Assigning the returned Workbook object to a variable makes it easier to refer to that workbook in later VBA statements.

Do I need the full path to open a workbook?
A full path is the clearest choice because it identifies the exact file. You can construct a shorter path relative to the macro workbook, but that works only when the expected file is in the specified relative location and the macro workbook has been saved.

Can VBA open a file from OneDrive or a network folder?
Yes, if Excel can access the file through a valid local or network path and the signed-in user has permission. For cloud files, use a synchronized local path where appropriate; a sharing URL may not work as a filename for Workbooks.Open.

Can I run this macro in Excel for the web?
No. Excel for the web can open and edit many macro-enabled workbooks, but it does not run VBA macros. Open the file in desktop Excel to run the procedure.

How do I open the workbook without changing the original?
Pass the read-only option when opening it. Read-only mode helps prevent edits being saved to that original instance, but keep a backup if other macro steps might alter data elsewhere.

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.