Excel VBA Open a Given File: Code and Options

VBA & macros · Updated November 19, 2026

Yes. In Excel for Microsoft 365 on Windows, VBA opens a specific workbook with the Workbooks.Open method, supplying its full file path. Use a fixed path when the file is known, or show a file picker when someone should choose which workbook to open.

What Workbooks.Open Does

Workbooks.Open asks Excel to open a workbook and returns a Workbook object that your macro can use. It can open files in different folders, including Excel workbooks and other formats Excel supports. The path is the key detail: a file name such as January.xlsx is not enough if Excel cannot determine which folder contains it.

A full Windows path includes a drive letter and folders, for example C:\Reports\January.xlsx. A shared network file may use a UNC path such as \\Server\Finance\January.xlsx. When the macro is stored in one workbook and the target file is beside it, you can build a path relative to the macro workbook instead of hard-coding a drive or user-specific folder.

Before running a macro that opens or otherwise acts on workbooks, save a copy of any important workbook. VBA actions generally cannot be undone with Ctrl+Z. Opening a file is not usually destructive by itself, but later macro steps could change or save data.

Open a Workbook from a Known Path

For a file at a predictable location, assign its full path to a string and pass it to Workbooks.Open. This example checks that the file can be found, opens it read-only, and avoids automatically updating external links:

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

    ' Change this to the workbook's actual full path.
    filePath = "C:\Reports\January.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, ReadOnly:=True)

    MsgBox "Opened " & openedBook.Name, vbInformation
    Exit Sub

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

The Dir$ check catches a common mistake before Excel tries to open the workbook. It does not guarantee that the file is readable or valid: permissions, a damaged file, a password, or a network problem can still prevent opening it. The error handler displays Excel’s error description instead of letting the macro stop without a helpful message.

ReadOnly:=True is useful when the macro only needs to inspect or copy information. Omit that argument, or set it to False, when the user should be able to edit the workbook. UpdateLinks:=0 tells Excel not to update external workbook links during the open operation. It does not remove links; it avoids retrieving updated values from linked files as the workbook opens.

Let the User Choose the File

If the location or file name changes, a file picker is usually more convenient than asking users to edit a path in the VBA code. Application.GetOpenFilename displays a selection dialog and returns the chosen path. It does not open the workbook itself, so the macro must pass that path to Workbooks.Open.

Sub ChooseAndOpenWorkbook()
    Dim selectedPath As Variant
    Dim openedBook As Workbook

    ' Ask the user to select an Excel workbook.
    selectedPath = Application.GetOpenFilename( _
        FileFilter:="Excel workbooks (*.xls*),*.xls*", _
        Title:="Choose a workbook")

    ' The dialog returns a Boolean False if the user cancels.
    If VarType(selectedPath) = vbBoolean Then Exit Sub

    On Error GoTo OpenFailed
    Set openedBook = Workbooks.Open( _
        Filename:=CStr(selectedPath), ReadOnly:=True)

    MsgBox "Opened " & openedBook.Name, vbInformation
    Exit Sub

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

The filter includes common Excel extensions beginning with .xls, including formats such as .xlsx, .xlsm, and .xlsb. The dialog’s appearance and available locations can vary by operating system and Excel version. On a Mac, VBA is supported in desktop Excel, but paths and the file-selection dialog can differ from Windows. Excel for the web does not run VBA macros.

Choose the Right Open Options

Workbooks.Open has optional arguments for how Excel should handle a workbook. It is better to specify only options that match the task than to copy a long list without understanding its effects. Common choices include:

  • ReadOnly:=True: Opens the workbook without intending to save changes back to that file. Choose this when the macro only reads data or when you want to protect the source from accidental edits.
  • UpdateLinks:=0: Prevents Excel from updating external workbook links during opening. If current linked values are required, decide deliberately how those links should be updated.
  • Password:=...: Supplies a password for a workbook that requires one to open. Avoid putting a real password directly in code that other people can inspect; VBA code is not a secure password vault.
  • Notify:=True: Can ask Excel to notify the user when a file that cannot be opened for writing becomes available. This is relevant to shared or locked files, not ordinary read-only inspection.

Read-only mode is not the same as making a file private or encrypted. It is an opening choice, and it does not replace appropriate file permissions or a protected backup. Also, a workbook may contain formulas linked to other workbooks; opening it without updating links can leave displayed values unchanged from the last saved calculation.

Use a Reliable File Path

Hard-coded paths are simple, but they can break when another person runs the macro under a different Windows account, when a folder is renamed, or when a file is moved. If the target workbook lives next to the workbook containing the macro, build the location from ThisWorkbook.Path, which refers to the file that stores the VBA code.

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

    ' ThisWorkbook.Path is the folder containing this macro workbook.
    filePath = ThisWorkbook.Path & Application.PathSeparator & "Source.xlsx"

    On Error GoTo OpenFailed
    Set openedBook = Workbooks.Open(Filename:=filePath, ReadOnly:=True)
    Exit Sub

OpenFailed:
    MsgBox "Could not open " & filePath & ": " & Err.Description, vbExclamation
End Sub

Application.PathSeparator supplies the separator Excel uses for the current environment. The macro workbook must already have been saved for ThisWorkbook.Path to identify a folder; an unsaved workbook has no normal file location. ThisWorkbook is also safer than relying on ActiveWorkbook: the active workbook can change as files open, while ThisWorkbook continues to mean the workbook containing the running code.

For files stored in cloud-synced folders or on a network, confirm that the path is available on the computer running the macro. A folder shown in one person’s OneDrive or mapped drive may have a different local path for someone else. A file picker can be the better choice when users have different folder layouts.

Handle Errors and Know Which Workbook Is Open

Opening can fail for reasons beyond a missing path. The file might be locked, password-protected, corrupt, in an unsupported format, or unavailable because a network connection dropped. Adding an error handler around the open operation lets you show a useful message or take a controlled next step.

Keep the returned workbook object, as the examples do, rather than assuming the opened file will remain the active workbook. For example, use openedBook.Worksheets("Summary") to refer to its Summary sheet. This makes code clearer when several workbooks are open and reduces the risk of reading or changing a sheet in the wrong file. Check that the expected worksheet exists before using it if files may have different layouts.

When a workbook is already open, avoid writing code that assumes the first item in the Workbooks collection is the target. Excel’s open workbooks can vary with the order users opened them. A stored object variable is a direct reference to the workbook your macro just opened and is generally the more dependable approach.

Opening Workbooks and Macro Safety

Only open files from sources you trust, especially if they are macro-enabled files such as .xlsm. A macro-enabled workbook can contain VBA, and Excel’s security settings determine whether its macros can run. Microsoft documents that macros are enabled by default for workbooks opened programmatically; code that opens an untrusted workbook should consider setting Application.AutomationSecurity to msoAutomationSecurityForceDisable before opening it, then restoring the previous setting afterward.

Do not treat a warning-free open as proof that a file is safe. Keep Office security protections enabled, avoid enabling content from unknown sources, and do not store sensitive passwords in visible VBA code. Also consider external links: they can request values from other files, which is why the examples explicitly use UpdateLinks:=0 when updating links is not needed.

Alternatives to VBA

VBA makes sense when opening a workbook is one step in a repeatable desktop Excel workflow. If the task is simply to open a file once, use Excel’s normal File > Open command instead. If the aim is to combine or transform data from files repeatedly, Power Query may be a better fit: in Excel for Microsoft 365 on Windows, start from Data > Get Data and choose a suitable source. Power Query imports or connects to data; it is not a replacement for opening a workbook window with VBA.

Excel for the web can open workbooks, but it cannot run VBA macros. A macro-enabled workbook can be stored or viewed online, yet code that depends on VBA must run in desktop Excel. On a Mac, desktop Excel supports VBA, though some Windows-specific paths, controls, or other automation features may need changes.

Frequently Asked Questions

What is the VBA command to open an Excel file?
Use Workbooks.Open and provide the file path, for example, Workbooks.Open Filename:="C:\Reports\January.xlsx". For reusable code, assign the returned workbook to a declared Workbook variable.

How do I open a file when I do not know its path?
Use Application.GetOpenFilename to let the user browse for a file. Check for cancellation, then pass the selected path to Workbooks.Open; the picker alone does not open the workbook.

Can VBA open a workbook as read-only?
Yes. Add ReadOnly:=True to the Workbooks.Open call. This is a practical choice when the macro needs to read data but should not edit the source file.

Why does my VBA code say the file cannot be found?
Check the complete path, spelling, extension, and folder permissions. Confirm that a mapped drive or cloud folder is available to the current user. A file picker can help distinguish an incorrect hard-coded path from a problem opening the file itself.

Does Excel for the web run an Open Workbook VBA macro?
No. Excel for the web does not run VBA. Run the macro in desktop Excel for Windows or Mac, bearing in mind that platform-specific paths or automation features may need adjustment.

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.