Excel VBA Open File Dialog: Code and Options

VBA & macros · Updated November 19, 2026

In Excel VBA for Windows, use Application.FileDialog(msoFileDialogFilePicker) to display a file picker, let the user choose a file, and then open it with Workbooks.Open. The dialog only gathers the selection; your macro must separately decide what to do with the chosen path.

The basic way to show a file picker

A file picker is useful when a macro needs a workbook or other file but the path may differ from one user or computer to another. Instead of hard-coding a location such as a particular person’s Documents folder, the macro asks the user to browse to the file. On Windows, the built-in Office FileDialog object provides the standard dialog.

To enter and run VBA in desktop Excel for Microsoft 365 on Windows:

  1. Save a copy of the workbook before running or changing a macro. VBA macros cannot be undone with Ctrl+Z.
  2. Save the macro-enabled workbook as an Excel Macro-Enabled Workbook (.xlsm) if it contains VBA.
  3. Open the Visual Basic Editor with Alt+F11, or use Developer > Visual Basic. If Developer is not visible, enable its tab in Excel Options.
  4. In the editor, choose Insert > Module, paste the procedure below, and run it from the editor or assign it to a button.

On a Mac, the Visual Basic Editor shortcut is usually Option+F11; some keyboards also require the Fn key. The Developer tab can also open the editor. Excel for the web does not run VBA macros, so use the desktop application.

Example: choose and open an Excel workbook

This procedure opens a single selected workbook as read-only. It offers common Excel file types, exits cleanly if the user presses Cancel, and displays a message if Excel cannot open the selected file. Read-only opening is a cautious default when the macro only needs to inspect or copy information.

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

    On Error GoTo OpenError

    Set filePicker = Application.FileDialog(msoFileDialogFilePicker)

    With filePicker
        .Title = "Select an Excel workbook"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Excel workbooks", "*.xlsx;*.xlsm;*.xlsb;*.xls"
        .Filters.Add "All files", "*.*"

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

    Set openedBook = Workbooks.Open(Filename:=selectedPath, ReadOnly:=True)
    Exit Sub

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

The .Show method displays the dialog and returns -1 when the user confirms. Any other result means the user did not confirm, so the procedure stops without trying to open a blank path. Once confirmed, .SelectedItems(1) supplies the first selected full path. The final line passes that path to Excel’s Workbooks.Open method.

The variable openedBook holds a reference to the workbook that was opened. If later code needs to read a sheet from that workbook, it can refer to openedBook rather than relying on which workbook happens to be active. This helps avoid confusing the workbook containing the macro with the workbook selected by the user.

How the dialog works

Application.FileDialog creates one of several Office dialogs. For choosing an existing file, the relevant option is msoFileDialogFilePicker. Other options serve different jobs: a folder picker chooses a directory, a save dialog helps select a destination and filename, and an open dialog is intended for opening existing items. For an Excel macro that will open a workbook, the file picker is a straightforward choice because the macro receives the selected path and explicitly controls how it is opened.

The dialog is modal: the macro waits while the user browses, selects, or cancels. The .Title property changes the prompt shown to the user. .AllowMultiSelect = False limits the selection to one item. The .Filters collection controls which file types are shown by default, while the selected item’s path is available through .SelectedItems. A filter is a convenience, not a guarantee that the chosen file is valid or safe to process; the macro should still handle errors.

Set filters, folder, and selection behavior

Filters make the dialog less confusing when a macro expects particular file types. For example, a workbook-processing tool can show Excel workbooks first and optionally let the user switch to All files. Use .Filters.Clear before adding your own filters, especially if the same dialog object is configured in more than one place. Extensions should be separated with semicolons in the filter pattern.

You can also set an initial location with .InitialFileName. For example, assigning a folder path ending in a backslash can start the dialog in that folder. Avoid assuming that every computer has the same user folder or mapped drive. A shared network location may be unavailable, and a hard-coded local path may not exist for another user. If the desired starting folder cannot be determined reliably, letting Excel use its normal starting location is often less fragile.

To allow multiple files, set .AllowMultiSelect = True. After the user confirms, loop through the selections rather than reading only item 1. For example, the loop can open each path, copy data, and close each workbook. Consider how the macro should behave if one file fails: it might report that file and continue, or stop immediately. Decide explicitly, because a single invalid or inaccessible file should not leave the macro in an unclear state.

Choosing a file is not the same as opening it

A common point of confusion is that displaying a picker does not automatically open the file. The picker returns a path; the macro’s next instruction determines whether to open it, import data, inspect its name, or do something else. This separation is useful: a macro can use a selected text file as an import source, or ask for a destination path without opening that destination as a workbook.

For a workbook, Workbooks.Open accepts additional options. The example uses ReadOnly:=True so the selected workbook is not opened for editing by this macro. Remove that argument only when the workflow genuinely needs to make changes and the user has appropriate access. Other options can affect link updating, passwords, and read-only recommendations; do not add such settings without understanding their effect. In particular, opening a file can involve external links or workbook content that deserves review.

If the goal is to open a non-Excel file in its usual Windows application, Excel VBA has other approaches, but they are different from opening a workbook with Workbooks.Open. For a CSV or text import, using Excel’s import features or opening the file as a workbook may produce different results, especially for dates, leading zeros, and delimiter handling. Choose the method based on the data you need to preserve.

Mac, Excel for the web, and compatibility

The main example targets desktop Excel for Microsoft 365 on Windows. VBA is not available in Excel for the web. On Mac, VBA is available in desktop Excel, but file-dialog behavior and support can differ by Office version and setup. If Application.FileDialog is unavailable or does not behave as expected on a particular Mac installation, Application.GetOpenFilename is a common alternative for asking the user to select a file.

GetOpenFilename returns a path when the user chooses a file and returns Boolean False when the user cancels. Check for that cancellation result before passing the value to Workbooks.Open. Its dialog is intended to return a filename; it does not itself open the workbook. Test a macro on the actual Mac and Excel versions that will use it, particularly if it relies on special folders, network paths, or file extensions.

For example, a Mac-oriented workflow can use Application.GetOpenFilename with an Excel-file filter, test whether the result is False, and then pass the returned path to Workbooks.Open. Keep the selection and opening steps separate, as in the Windows example. This makes cancellation handling clear and allows the macro to report a useful error if a selected file cannot be opened.

Common problems and safer handling

  • The dialog does not appear: Confirm that the code is running in desktop Excel and that macros are allowed by your organization’s security settings. Excel for the web cannot run VBA.
  • The macro errors after Cancel: Check the return value from .Show before accessing .SelectedItems(1) or opening a path.
  • The wrong kind of file is selected: Use a clear title and useful filters, then validate the selected path or attempt the intended operation inside error handling. A filter does not prove that a file’s contents match its extension.
  • The selected workbook is already open: Excel may activate or reuse an existing workbook window rather than create a separate copy. If the workflow depends on a new instance or particular workbook state, account for that deliberately.
  • A network or cloud path fails: The user may lack permission, be offline, or have a different synchronization path. Report the problem and avoid assuming that a displayed filename guarantees access.

Macros can alter files, and opening a workbook may expose the user to external links or other content. Keep the macro’s purpose visible, use read-only access when editing is unnecessary, and do not disable security controls just to make a dialog work. Only run macros and open files from sources the user is entitled to trust. Save a separate copy of the workbook containing your VBA before testing changes; macro actions generally cannot be reversed with Ctrl+Z.

Alternatives to a file dialog

A dialog is not always the best interface. If every user has the same controlled source, a configured path or a named cell containing the path may be simpler, though it should still be checked for availability. If the macro is part of a repeatable import process, Excel’s Power Query tools may be a better fit because users can refresh a managed data connection without running custom VBA. For a one-time manual task, File > Open or Data > Get Data may be enough and requires no macro at all.

Use a dialog when the person running the macro needs to choose among files at run time. Use a folder picker when the user should choose a location rather than a specific file. Use a save dialog when the task is to choose an output filename. Matching the dialog to the task reduces mistakes and makes the macro’s purpose easier to understand.

Frequently Asked Questions

How do I open a file dialog in Excel VBA?

For Windows desktop Excel, create a file picker with Application.FileDialog(msoFileDialogFilePicker), configure it, and call .Show. Check that the result is -1 before reading .SelectedItems(1).

How do I open the selected workbook?

Store the chosen path from .SelectedItems(1) in a string, then use Workbooks.Open(Filename:=selectedPath). Use ReadOnly:=True if the macro only needs to inspect the workbook.

How do I let users select multiple files?

Set .AllowMultiSelect = True before showing the dialog. After confirmation, loop from 1 through .SelectedItems.Count and process each selected path, with error handling for files that cannot be accessed.

Why does my VBA file picker return an error when I cancel?

Cancel means there is no selected item to read. Check the result of .Show first and exit the procedure unless it equals -1. On Mac, check whether GetOpenFilename returned Boolean False.

Can Excel for the web run a VBA file dialog?

No. VBA macros run in desktop Excel, not Excel for the web. Open the macro-enabled workbook in desktop Excel on Windows or Mac, and account for platform differences in dialog support and behavior.

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.