Open an Excel File Using VBA: Step-by-Step Guide

VBA & macros · Updated November 20, 2026

To open an Excel workbook with VBA, use the Workbooks.Open method and give it the file’s full path, for example Set wb = Workbooks.Open(Filename:="C:\Reports\MonthlyReport.xlsx"). In Excel for Microsoft 365 on Windows, press Alt+F11 to open the VBA editor, insert a standard module, add a macro, and run it. You’ll need the workbook’s location and permission to run macros. The steps below also show how to let someone choose a file and how to handle common errors.

What You’ll Need

  • Excel for Microsoft 365 desktop on Windows. VBA macros do not run in Excel for the web. Excel for Mac has a VBA editor too, though some dialog and file-path details differ.
  • The full path to the workbook you want to open, such as C:\Reports\MonthlyReport.xlsx, or a plan to use a file picker instead.
  • A workbook in which you can store and run VBA. Save a macro-containing workbook as an Excel Macro-Enabled Workbook (.xlsm); a regular .xlsx file cannot store VBA code.
  • A trusted source for the file. Do not lower Excel’s macro security settings to run code from an unknown workbook.

Step-by-Step

  1. Open the workbook where the macro will live. This is usually the workbook that will run the automation, not necessarily the workbook you plan to open. If the macro should be reusable from any workbook, you can instead store it in your Personal Macro Workbook. For a first attempt, use a new workbook and save it as .xlsm.
  2. Open the Visual Basic Editor. In Excel for Windows, press Alt+F11. You can also choose Developer > Visual Basic. If Developer is not visible, go to File > Options > Customize Ribbon, check Developer in the Main Tabs list, and select OK. On a Mac, use Tools > Macro > Visual Basic Editor, or the editor shortcut available for your keyboard and Excel version; Mac keyboard shortcuts can differ.
  3. Insert a standard module. In the editor, select the project for your macro workbook in Project Explorer. If Project Explorer is hidden, press Ctrl+R on Windows or use View > Project Explorer. Choose Insert > Module. A blank code window appears. A standard module is a suitable place for a macro you will run manually or call from another procedure.
  4. Add the file-path macro. Paste this procedure into the module. Replace the sample path with the full path to the workbook you want to open. Keep the quotation marks around the path, and check that the extension matches the real file.
Sub OpenWorkbookFromPath()
    Dim filePath As String
    Dim openedWorkbook As Workbook

    ' Change this to the workbook's full path.
    filePath = "C:\Reports\MonthlyReport.xlsx"

    ' Stop with a clear message if the file is missing.
    If Len(Dir(filePath)) = 0 Then
        MsgBox "File not found: " & filePath, vbExclamation
        Exit Sub
    End If

    On Error GoTo OpenError

    ' Open without updating external links automatically.
    Set openedWorkbook = Workbooks.Open( _
        Filename:=filePath, UpdateLinks:=0)

    MsgBox "Opened: " & openedWorkbook.Name, vbInformation
    Exit Sub

OpenError:
    MsgBox "Excel could not open the workbook: " & _
        Err.Description, vbExclamation
End Sub
  1. Understand the path and link setting. The example uses a Windows path, with backslashes between folders. Dir(filePath) checks that Excel can find a file at that location before trying to open it. UpdateLinks:=0 tells Excel not to update external workbook links automatically during opening. If you need current linked values, decide how to update them safely rather than removing that setting without considering the prompts or data sources.
  2. Run the macro. Return to Excel with Alt+F11 or switch windows. Save a copy of your workbook before running the macro: VBA changes and file operations cannot be undone with Ctrl+Z. In Excel, press Alt+F8, select OpenWorkbookFromPath, then choose Run. If Excel displays a security warning, only enable macros when you trust the workbook and its source. The target file should open in the same Excel instance.
  3. Check that the intended file opened. Look at the workbook title bar or the sheet tabs. The macro also displays the opened workbook’s name. The variable openedWorkbook refers to the workbook returned by Workbooks.Open; you can use it in later code to refer to that particular file instead of relying on whichever workbook happens to be active.
  4. Use a file picker when the path changes. If the person running the macro should choose a workbook, use this alternative procedure instead of the fixed-path macro. It displays Excel’s file selection dialog, accepts Excel workbook extensions, and exits cleanly if the user cancels.
Sub ChooseAndOpenWorkbook()
    Dim selectedFile As Variant
    Dim openedWorkbook As Workbook

    ' Let the user choose an Excel workbook.
    selectedFile = Application.GetOpenFilename( _
        FileFilter:="Excel Workbooks (*.xls*), *.xls*", _
        Title:="Select a workbook to open")

    ' GetOpenFilename returns False if the user cancels.
    If VarType(selectedFile) = vbBoolean Then Exit Sub

    On Error GoTo OpenError
    Set openedWorkbook = Workbooks.Open( _
        Filename:=CStr(selectedFile), UpdateLinks:=0)

    MsgBox "Opened: " & openedWorkbook.Name, vbInformation
    Exit Sub

OpenError:
    MsgBox "Excel could not open the workbook: " & _
        Err.Description, vbExclamation
End Sub
  1. Save the macro workbook. In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) if it is not already saved in that format. Close and reopen the macro workbook if you want to check that the code remains available. The workbook being opened can have a different format, such as .xlsx, .xlsm, or .xlsb.

For a Mac, use a Mac-style path when hard-coding a local file, for example /Users/YourName/Documents/MonthlyReport.xlsx. A file picker is often easier than maintaining paths that differ between Windows and Mac. The macro code itself is generally usable in desktop Excel on both platforms, but dialogs, permissions, network locations, and keyboard shortcuts may vary by Excel version and macOS settings.

Common Mistakes

  • Using only a file name. MonthlyReport.xlsx is not a dependable location unless Excel’s current folder happens to contain that file. Use a full path or ask the user to select the file.
  • Typing the path incorrectly. A missing folder, misspelled file name, wrong extension, or extra character can make the file appear missing. Copy the path from File Explorer and check that the file is not merely named with a similar extension.
  • Saving the macro as .xlsx. An .xlsx workbook does not retain VBA code. Save the workbook that contains the macro as .xlsm or another format that supports macros.
  • Using ActiveWorkbook when you mean the file just opened. The active workbook can change as code runs or a user clicks another window. Keep the result of Workbooks.Open in a Workbook variable and refer to that variable for subsequent actions.
  • Assuming the file picker opens the file. Application.GetOpenFilename returns a selected file name; it does not open the workbook. Pass the returned path to Workbooks.Open, as the example does.
  • Automatically updating links without checking them. External links may point to other files or locations. The sample suppresses automatic link updating with UpdateLinks:=0; review the workbook’s link requirements before changing that choice.

Tips

  • Give variables meaningful names, such as openedWorkbook and filePath. This makes it easier to see which workbook later statements should affect.
  • Keep the opening step separate from later work. First confirm that the file opens; then add actions such as selecting a worksheet or reading a range. This makes it easier to identify whether an error comes from the path or from a later instruction.
  • If you regularly open one known file, store its path in a worksheet cell or configuration setting rather than editing VBA each time. Validate the value before using it, especially if other users can change that cell.
  • Use the file picker when files move between folders or users have different folder names. A selected path is more portable than a hard-coded personal Documents folder.
  • Consider whether the target workbook should be read-only. You can pass ReadOnly:=True to Workbooks.Open when the macro only needs to inspect it and should avoid editing the source. If changes need to be saved, confirm the intended save location and format first.
  • Do not add a password to the code as a quick fix for a protected file. Password-protected workbooks require the appropriate authorized password and the relevant Workbooks.Open arguments. Handle credentials securely and avoid sharing a macro that exposes them.
  • On Windows, Alt+F11 opens the VBA editor and Alt+F8 opens the Macro dialog. On Mac, shortcuts differ by keyboard and Excel release; use the Tools > Macro menu or Excel’s Help search if a shortcut is intercepted by macOS.

Troubleshooting

“File not found” appears. Copy the full path again and verify it in File Explorer. Confirm that the drive is connected, the folder is accessible, and the extension is correct. If the file is on a network share, make sure you can open that location manually. A mapped drive letter may not be available to another user or in a different sign-in session; ask for the correct accessible path or use the file picker.

Excel reports that the file format or extension is invalid. The extension may not match the actual file type, or the file may be damaged or incomplete. Open it manually in Excel first. If Excel displays a repair or format warning, resolve that issue before automating the open. Do not simply rename the extension to make an unrelated file look like an Excel workbook.

The macro does not appear in Alt+F8. Confirm that the code is in a standard module and that the procedure is a public, parameter-free Sub, not code placed in a worksheet or ThisWorkbook module. Check that the macro workbook is open and saved as .xlsm. If Excel has blocked macros, follow your organization’s trusted-document process; do not disable security protections globally.

The file opens but the data looks out of date. The sample uses UpdateLinks:=0, so external links are not refreshed as part of opening. Check whether the workbook relies on linked files or connections. Update links only when you recognize the sources and know that refreshing them is appropriate.

The file opens read-only or shows a password prompt. Another person may have it locked, the file may be marked read-only, or you may lack write permission. If viewing is enough, explicitly request read-only access. If editing is necessary, check permissions and close any other copy you own. For a password prompt, use the correct authorized password rather than trying to bypass protection.

The macro works on Windows but not on Mac. Check that the code uses a valid Mac path or use the file picker. Also confirm the workbook is stored somewhere Excel for Mac can access, including any required macOS file permissions. The Windows drive path beginning with a letter such as C:\ will not identify a local Mac folder.

Frequently Asked Questions

What is the basic VBA command to open an Excel file?
Use Workbooks.Open with the file’s path, such as Set wb = Workbooks.Open(Filename:="C:\Reports\MonthlyReport.xlsx"). Declare wb as a Workbook variable and use a real, accessible path.

Can VBA open a workbook without a fixed path?
Yes. Use Application.GetOpenFilename to let the user choose a file, then pass the returned path to Workbooks.Open. Check for cancellation before trying to open the selection.

Can I run this VBA in Excel for the web?
No. Excel for the web can open and edit many macro-enabled workbooks, but it cannot create, edit, or run VBA macros. Open the file in the desktop Excel application to run the code.

Will opening an .xlsm file automatically run its macros?
Do not assume that it will. Macro execution depends on Excel’s security settings, the file’s trust status, and how it is opened. Only enable or run macros from a source you trust, and follow workplace security requirements.

How do I open the workbook as read-only?
Set the ReadOnly argument in Workbooks.Open to True, for example Set wb = Workbooks.Open(Filename:=filePath, ReadOnly:=True). This is useful when the macro only needs to inspect the workbook, not change and save it.

Can the macro open a workbook that is already open?
Excel may activate or reuse the open workbook rather than creating a separate independent copy. If your automation needs to avoid duplicate work, check the open workbooks by name or full path before calling Workbooks.Open. Make sure your later code refers to the intended workbook variable.

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.