Excel VBA Open File Dialog: A Practical Guide

VBA & macros · Updated November 19, 2026

To show an Open dialog in Excel VBA, use Application.FileDialog(msoFileDialogFilePicker), set its title and filters, and call .Show. The dialog lets a user choose a file; your macro must then read the selected path and decide whether to open or otherwise process it.

What the VBA file dialog does

Excel’s FileDialog object displays a standard file-picker window, so users can browse folders instead of typing a path into a worksheet or a VBA prompt. The file picker does not open a file by itself. Calling .Show displays the window and returns a result; your code checks that result and then uses the selected path.

For choosing an existing file, the relevant dialog type is msoFileDialogFilePicker. Other types include a folder picker and a save-as dialog, but those serve different tasks. A picker is useful when a macro needs an input workbook, CSV file, image, or other existing file. It is also more forgiving than asking a user to enter a full path, which can be mistyped or differ between computers.

The example below is for Excel for Microsoft 365 on Windows. The FileDialog interface is part of the Office object model; behavior and file access can vary with Office version, Mac permissions, and organizational security settings.

A basic VBA example

This macro asks the user to select an Excel workbook and opens it. If the user clicks Cancel, it exits without trying to open anything. Before running any macro, save a copy of the workbook: VBA changes cannot be undone with Ctrl+Z, and a macro may alter data or open an unintended file.

Sub ChooseAndOpenWorkbook()
    Dim fd As FileDialog
    Dim selectedPath As String

    ' Create a dialog for selecting an existing file.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

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

        ' Show returns -1 for OK and 0 for Cancel.
        If .Show <> -1 Then
            MsgBox "No file was selected.", vbInformation
            Exit Sub
        End If

        selectedPath = .SelectedItems(1)
    End With

    Workbooks.Open Filename:=selectedPath
End Sub

The variable fd refers to the dialog, while selectedPath stores the chosen file’s full path. With .AllowMultiSelect = False, there is one selected item, so .SelectedItems(1) is the path to use. The filter limits the displayed choices to common Excel workbook extensions; it does not convert a file or guarantee that a selected file is safe.

How to add and run the macro

  1. In Excel for Windows, enable the Developer tab if it is not visible: choose File > Options > Customize Ribbon, then select Developer.
  2. Press Alt+F11 to open the Visual Basic Editor. In the editor, choose Insert > Module.
  3. Paste the procedure into the module and save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep the VBA code in it.
  4. Return to Excel and run the procedure from Developer > Macros, or run it in the editor with the cursor inside the procedure.

Excel may block macros depending on the file’s source and your Trust Center or organization settings. Do not lower security settings just to run unknown code. If the macro is stored in a workbook, remember that saving as .xlsx removes VBA code; use a macro-enabled format to retain it.

Understanding the dialog settings

Several properties let you tailor the picker without changing its basic behavior:

  • .Title sets the text shown at the top of the dialog. A specific title, such as “Choose the monthly source file,” helps users understand what the macro expects.
  • .AllowMultiSelect controls whether the user may choose more than one file. Set it to True only if the rest of the macro is written to handle multiple paths.
  • .Filters.Clear removes the existing filter entries, and .Filters.Add adds a description and one or more filename patterns.
  • .InitialFileName can provide an initial folder or filename suggestion. For example, a macro can start in a known project folder, though that folder may not exist on another user’s computer.

A filter is a convenience, not a validation rule. Users may be able to change the file-type selection in the dialog, and a filename extension does not prove what a file contains. If the macro depends on a particular format, check the selected path or handle errors when processing the file.

Allowing multiple files

For batch processing, set .AllowMultiSelect = True. After the user clicks OK, loop through .SelectedItems rather than assuming the first selection is the only one. Keep the cancellation check: when the user cancels, there are no selected paths to process. A simplified pattern inside the same With fd block is:

.AllowMultiSelect = True
If .Show <> -1 Then Exit Sub

For Each selectedPath In .SelectedItems
    ' Process this selected file here.
Next selectedPath

For that loop, declare selectedPath as a Variant or String before using it. The procedure must also define what “process” means: opening each workbook, importing a range, or copying files are different operations. If you open many workbooks, consider how the macro will handle files that are already open, password-protected, damaged, or in an unexpected format.

Opening, importing, or only checking a file

After the user chooses a file, the selected path can be passed to Workbooks.Open, as in the full example. If the goal is to import data, the macro may instead open the source workbook, copy or read the required data, and close the source. If the goal is only to ask which file to use, omit the Workbooks.Open line and display or store selectedPath.

This distinction matters because showing the dialog is not the same as opening a workbook. It also lets you add checks before opening: confirm the expected extension, ask for confirmation, or test whether a path is available. Opening a workbook can trigger prompts or security protections, and a workbook containing macros may be subject to Excel’s macro security settings. Treat files selected from email or unfamiliar locations cautiously.

FileDialog compared with GetOpenFilename

Application.GetOpenFilename is another common way to prompt for a file. It can be concise and returns the selected filename without opening the file. Its return value is a Variant: a path when the user selects a file, or Boolean False when the user cancels. Code using it must test for cancellation before treating the result as a path.

FileDialog is often preferable when you want to set a clear dialog title, manage filter entries, or allow multiple selections through a dialog property. GetOpenFilename can be a reasonable alternative for a small macro that needs a simple single-file prompt. Neither approach automatically validates the file’s contents, and neither opens the chosen file unless you add code to do so.

Common problems and fixes

  • The code errors at FileDialog: Check that the code is running in Excel VBA and that the variable is declared as FileDialog. This Office object is not a general-purpose feature available identically in every VBA host.
  • Cancel causes an error: Check the return value from .Show before reading .SelectedItems(1). The dialog returns -1 when the user confirms and 0 when they cancel.
  • The wrong files appear: Clear old filters before adding the intended filter. Check the extension patterns and remember that users may switch the filter in the dialog.
  • The file cannot be opened: The path may be unavailable, the file may be damaged or protected, or Excel may not recognize its format. Add suitable error handling if the macro must report these cases clearly.
  • The macro works on one computer but not another: A hard-coded starting folder may not exist for other users, and network locations may require different access. Use a neutral starting location or allow the user to browse.

For production code, error handling should distinguish a user cancellation from a genuine failure to open the chosen file. Avoid a broad error handler that silently ignores every problem: it can make a failed import look successful.

Mac and platform considerations

The steps and code here target Microsoft 365 on Windows. Excel for the web does not run VBA macros, so the procedure must be used in the desktop application. Excel for Mac has VBA support, but keyboard shortcuts, file permissions, and dialog behavior can differ by version and system configuration. Test a macro on the actual Mac version and confirm that it can access the intended folders; do not assume a Windows path such as a drive letter is valid on Mac.

On Windows, Alt+F11 opens the Visual Basic Editor. On Mac, the editor shortcut can depend on keyboard and function-key settings; use Excel’s Developer tab or the application menus if the shortcut differs. Sharing a workbook between platforms also calls for checking any Windows-specific path assumptions and reviewing how the organization handles downloaded or network files.

Frequently Asked Questions

How do I open a file picker in Excel VBA?
Use Set fd = Application.FileDialog(msoFileDialogFilePicker), configure the dialog, and call fd.Show. Check its result before reading the selected path.

Does FileDialog open the selected workbook automatically?
No. It displays a picker and returns the user’s choice. Add a line such as Workbooks.Open Filename:=selectedPath if the macro should open that workbook.

How do I let users select more than one file?
Set .AllowMultiSelect = True, check that .Show returned -1, then loop through .SelectedItems. The macro must be designed to process each chosen path.

What does FileDialog.Show return when the user cancels?
For this dialog, .Show returns 0 on cancellation and -1 when the user confirms. Check the result before accessing the selected items.

Can I use the macro in Excel for the web?
No. Excel for the web does not execute VBA. Open the macro-enabled workbook in the desktop version of Excel to run VBA, subject to its macro security settings.

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.