VBA Code to Open an Excel File from a Folder Safely

VBA & macros · Updated November 20, 2026

To open an Excel workbook from a folder with VBA, call Workbooks.Open with the workbook’s full file path. For example, the macro below checks that the file exists and opens it read-only; save a copy of your workbook before running any macro, because VBA actions cannot be undone with Ctrl+Z.

Basic VBA code for a full file path

In this example, the workbook is named MonthlySales.xlsx and is stored in C:\Reports. Change both values to match your folder and file. The macro can go in a standard module in the workbook that will run it.

Sub OpenWorkbookFromFolder()
    Dim folderPath As String
    Dim fileName As String
    Dim fullPath As String
    Dim openedBook As Workbook

    ' Set these values to the folder and workbook you want to open.
    folderPath = "C:\Reports"
    fileName = "MonthlySales.xlsx"

    ' Add a final backslash if the folder path does not already have one.
    If Right$(folderPath, 1) <> "\" Then
        folderPath = folderPath & "\"
    End If
    fullPath = folderPath & fileName

    ' Stop with a useful message if the file is missing or the path is wrong.
    If Len(Dir$(fullPath)) = 0 Then
        MsgBox "File not found: " & fullPath, vbExclamation
        Exit Sub
    End If

    ' Open without changing the source workbook.
    Set openedBook = Workbooks.Open(Filename:=fullPath, ReadOnly:=True)
End Sub

In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, paste in the code, and run OpenWorkbookFromFolder. On a Mac, use Option+F11 (on some keyboards, Fn+Option+F11) to open the editor; the VBA editor and macro support are available in desktop Excel, but the example’s Windows-style path must be changed to a Mac path.

What the macro is doing

Workbooks.Open is the Excel VBA method that opens a workbook. The Filename argument tells Excel which file to open. A full path includes the drive or network location, folders, and filename with its extension. For example, C:\Reports\MonthlySales.xlsx identifies a workbook more precisely than MonthlySales.xlsx, which does not say where the file is.

The code stores the opened workbook in openedBook. That reference is useful when later steps should work on the file just opened rather than whichever workbook happens to be active. For instance, code can refer to openedBook.Worksheets(1) to address its first worksheet. If you omit the variable, Excel still opens the file, but later code that relies on ActiveWorkbook can accidentally act on a different workbook if focus changes.

Dir$ checks whether a file matching the supplied path can be found. The check helps replace a confusing open error with a clear message, but it is not a guarantee that the file can be opened: permissions, file damage, a password, or another issue can still stop Excel.

Use a file picker instead of typing the path

If the folder or filename changes often, a file picker lets the person running the macro choose the workbook. This avoids editing the path in the code each time. The following version opens the selected file in the usual editable mode, so only use it when editing the chosen workbook is intended.

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

    ' Ask the user to select one Excel 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

    ' Open the selected workbook.
    Set openedBook = Workbooks.Open(Filename:=selectedPath)
End Sub

Clicking Cancel exits the macro before it tries to open anything. The filter helps the user find common Excel workbook formats, but it is not a security check: selecting a file does not make its contents trustworthy. If the workbook contains macros, Excel’s macro security settings still apply. The file picker is also useful when users store files in different locations, such as separate project folders or synced folders.

Build a path relative to the macro workbook

A macro can also locate a file in a known subfolder next to the workbook containing the code. This is often easier to move between computers than a fixed path such as C:\Reports. For example, if the macro workbook is saved in a folder and the target workbook is in a subfolder named Input, build the path from ThisWorkbook.Path:

Sub OpenWorkbookFromSiblingFolder()
    Dim fullPath As String
    Dim openedBook As Workbook

    ' ThisWorkbook means the workbook that contains this macro.
    If Len(ThisWorkbook.Path) = 0 Then
        MsgBox "Save the macro workbook before running this macro.", vbExclamation
        Exit Sub
    End If

    fullPath = ThisWorkbook.Path & Application.PathSeparator & _
               "Input" & Application.PathSeparator & "Source.xlsx"

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

    Set openedBook = Workbooks.Open(Filename:=fullPath, ReadOnly:=True)
End Sub

ThisWorkbook refers specifically to the workbook that holds the VBA code; ActiveWorkbook means whichever workbook is currently active. That distinction matters if the macro opens another file. Application.PathSeparator supplies the path separator used by the current Excel environment, but the target folder still needs to exist at that location. If the macro workbook has never been saved, its path is empty, which is why the example checks before building the file path.

Choose whether to open read-only or editable

The first sample uses ReadOnly:=True. Excel opens the workbook for viewing without intending to save changes back to that source file. This is a sensible default for reporting, importing data, or inspecting a workbook that should remain unchanged. To open it for editing, remove that argument or set it to False; Excel may still show a read-only state if the file or its location prevents writing.

Another optional argument is UpdateLinks. Setting UpdateLinks:=0 tells Excel not to update external references when opening the workbook. That can prevent automatic refreshes of linked values, but it also means the displayed linked data may be out of date. If current linked values are required, decide how those links should be handled rather than turning updates off by habit.

Files can have different extensions, including .xlsx, .xlsm, .xlsb, and the older .xls format. Use the actual filename and extension. If the file is macro-enabled, opening it does not automatically mean its VBA code should be enabled; follow your organization’s security policies and Excel’s warnings.

Common errors and how to fix them

  • File not found: Check spelling, extension, folder names, and the drive letter. Windows paths are commonly written with backslashes, such as C:\Reports\Source.xlsx. A mapped drive might not be available under a different Windows account or when Excel is launched in another context.
  • Permission or access error: Confirm that the account running Excel can read the folder. For a shared or network location, verify that the connection is available and that the file has not been moved or renamed.
  • The wrong workbook is being changed: Store the return value from Workbooks.Open in a Workbook variable and refer to that variable in later code. Do not assume that ActiveWorkbook will always be the intended file.
  • Excel says the file is already open: A workbook may already be open in the same Excel instance. Check the open workbooks before opening it again, or decide whether the macro should reuse the existing workbook. Do not blindly open multiple copies.
  • The file opens read-only: The macro may request read-only access, another user may have it locked, or the folder may not allow edits. Remove the read-only argument only if editing is appropriate; it cannot override permissions or file locks.
  • A prompt appears about links or security: Review the workbook’s source and the prompt. VBA can specify some opening options, but it should not be used to bypass security decisions or silently trust unknown files.

For production macros, add error handling around the open operation so an unexpected problem can be reported clearly. A simple existence test does not catch every failure. For example, a file may exist but be corrupted, password-protected, or inaccessible at the moment the macro runs. Error handling should describe the failure and leave the user in control rather than hiding the problem.

Windows and Mac path differences

The examples’ fixed path uses a Windows drive letter, such as C:\Reports\Source.xlsx. A Mac file path generally uses a POSIX-style path, for example /Users/Name/Reports/Source.xlsx. Do not paste a Windows drive path into a Mac version of Excel and expect it to identify the same folder. If a workbook must be used on both platforms, a path based on the macro workbook and Application.PathSeparator can help, but folder locations and access permissions still need to be considered.

Excel for the web can open and edit many workbooks, but it does not run VBA macros. Use the desktop Excel application to run this code. On Windows, the standard editor shortcut is Alt+F11; on Mac, use Option+F11, sometimes with Fn depending on keyboard settings. Ribbon labels and editor behavior can vary slightly by platform and version.

Safety and macro preparation

Only run VBA from a workbook and source you trust. A macro that opens a workbook can expose the user to content in that file, and a macro-enabled workbook can contain its own code. Keep Excel’s macro security protections enabled, and do not enable macros simply because a file was selected in a picker or arrived in a familiar folder.

Before running or changing a macro, save a separate copy of the workbook containing the code. VBA actions generally cannot be reversed with Ctrl+Z, even if the macro changes cells or opens files. On Windows, save code that must be retained in a macro-enabled workbook such as .xlsm (or another macro-supporting format); saving as .xlsx does not preserve VBA code. The desktop Mac version also supports VBA, though file paths and some platform-specific behavior differ.

Frequently Asked Questions

What is the simplest VBA command to open an Excel file?
Use Workbooks.Open Filename:="C:\Reports\Source.xlsx" in a macro, replacing the example path with the real full path. A full path is the least ambiguous choice when the file always lives in the same place.

Can VBA open a workbook without knowing its filename in advance?
Yes. Use Application.FileDialog(msoFileDialogFilePicker) to let the user browse to and select a workbook, then pass the selected path to Workbooks.Open.

How do I open a workbook read-only?
Add ReadOnly:=True to the Workbooks.Open call. This requests read-only access, which is useful when the macro only needs to inspect or import information.

Why does VBA say it cannot find my file?
Check the complete path, filename, extension, and permissions. Also confirm that a mapped drive or network folder is available to the account running Excel. A path that works on one computer may not exist on another.

Can I run this macro in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in the desktop version of Excel to use the VBA editor and run the code.

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.