Excel VBA: Open a File Dialog to Select a Folder

VBA & macros · Updated November 19, 2026

To let someone choose a folder in Excel VBA, use Application.FileDialog(msoFileDialogFolderPicker). Show the dialog, check whether the user clicked OK, and read the chosen directory from .SelectedItems(1); if they cancel, exit without using a path.

What the folder picker does

Excel’s FileDialog can present different kinds of dialogs, including one for opening a file, one for saving a file, and one for selecting a folder. For a directory-selection task, the relevant type is msoFileDialogFolderPicker. The user navigates to a directory and confirms the selection, and VBA receives the directory path rather than a file name.

This distinction matters when a macro will process several files in a chosen directory. A file-open dialog asks for one or more specific files; a folder picker asks for the containing directory. The selected path can then be used to build file paths, list files, or pass the directory to another part of the macro.

The picker does not itself open, copy, or change any files. It only obtains the user’s selection. Your code determines what happens next, so you can ask for a folder without automatically reading or modifying its contents.

A basic VBA example

This macro displays a folder-selection dialog, handles Cancel, and shows the selected path. It uses Excel’s built-in Office object library, so there is no need to add a separate reference in a standard Excel VBA project.

Sub ChooseFolder()
    Dim folderDialog As FileDialog
    Dim selectedFolder As String

    ' Create a dialog for selecting a folder.
    Set folderDialog = Application.FileDialog(msoFileDialogFolderPicker)

    With folderDialog
        .Title = "Select a folder"
        .AllowMultiSelect = False

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

        selectedFolder = .SelectedItems(1)
    End With

    MsgBox "Selected folder:" & vbCrLf & selectedFolder, vbInformation
End Sub

In the Visual Basic Editor, insert this into a standard module, such as one created with Insert > Module. The FileDialog variable represents the dialog; .Show displays it; and the first item in .SelectedItems contains the chosen folder. Because multiple selection is disabled, the code expects one result.

The test for .Show is essential. A confirmed dialog returns -1, while Cancel returns 0. Without the test, code that immediately reads .SelectedItems(1) may try to access an item that does not exist. The example exits the macro before that can happen.

How to run the macro

  1. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if it needs to retain VBA code.
  2. Open the VBA editor with Alt+F11 on Windows. If the Developer tab is visible, Developer > Visual Basic opens the editor as well.
  3. Choose Insert > Module, paste the procedure into the module, and return to Excel.
  4. Run it from the VBA editor with F5 while the cursor is inside the procedure, or use Developer > Macros, select ChooseFolder, and choose Run.
  5. Choose a directory in the dialog and confirm. The example displays the resulting path in a message box.

If Developer is not shown on Windows, enable it through File > Options > Customize Ribbon by checking Developer. Macro security settings may also prevent a downloaded or untrusted workbook from running code. Only enable macros in files you trust.

Before running a macro that will later process or change files, save a separate copy of the workbook. VBA actions generally cannot be reversed with Ctrl+Z, so the normal Excel undo history is not a safety net for macro changes.

Use the chosen path in your own code

The variable selectedFolder holds the path after the user confirms. In the example, the message box makes that value visible; in a working macro, replace the message box with the operation that needs the directory. For example, the code could pass the path to a procedure that imports files, or store it in a worksheet cell for later use.

Keep the folder selection and the file-processing logic conceptually separate. First confirm the user chose a directory; then validate that the location is suitable; only then begin processing. A selected folder might be empty, unavailable, read-only, or contain files in a format your macro does not expect. The picker cannot verify those assumptions for you.

When your code constructs a full file name from the directory and a file name, include the operating system’s path separator exactly once. Excel VBA provides Application.PathSeparator, which is preferable to assuming every path uses the same character. For instance, a macro can combine the selected directory, the separator, and a known file name, then check whether the resulting file exists before opening it.

A path should be treated as a string, not as a workbook or file object. Selecting a directory does not open the directory in Excel, and assigning the result to a cell does not grant the workbook special access. Any later file operation still needs suitable VBA code and permission from the operating system.

Changing the dialog’s title and starting location

The .Title property gives users a more useful instruction than a generic prompt. Set it to a short action-oriented phrase such as “Choose the monthly reports folder” or “Select the source directory.” A clear title can prevent someone from choosing a destination when the macro expects a source.

You can also set .InitialFileName to suggest a starting location. For example, a macro may use a known, existing directory relevant to the workbook. Treat that as a convenience rather than a guarantee: paths may differ between computers, and a suggested folder may have been moved or may not be accessible. If the workbook has not yet been saved, ThisWorkbook.Path is empty, so do not blindly assume it identifies a usable starting folder.

For the simplest and most portable behavior, omit an initial location and let the user navigate. If you do set one, build and validate it deliberately, and test it on the Excel versions and operating systems where the workbook will run. Avoid hard-coding a personal user profile or a drive letter unless the workbook is specifically designed for that environment.

Windows, Mac, and Excel for the web

This example is written for desktop Excel for Microsoft 365 on Windows. Excel for the web does not run VBA macros, so a workbook opened in a browser cannot execute this procedure there. The workbook must be opened in a desktop version of Excel to run its VBA code.

Excel for Mac supports VBA, but dialog behavior, available folders, and path conventions can differ from Windows. A macro intended for both platforms should avoid assumptions such as a Windows drive-letter path, and should be tested on the actual Mac and Office version users will use. Keyboard shortcuts also vary by Mac keyboard; use the Visual Basic command on the Developer tab if a shortcut is unavailable.

Office’s FileDialog interface is generally the direct VBA approach on desktop Excel, but an organization’s security policy, operating-system permissions, or a particular Office build may affect what the user can access. The user still needs permission to browse the chosen location, and selecting a network or cloud-synced folder does not guarantee that it is online or writable.

Folder picker versus other dialog methods

Use the folder picker when the user should choose a directory. Application.GetOpenFilename is for selecting a file name, not for returning a folder as the selection. Similarly, Application.GetSaveAsFilename helps choose a file name and save location; it is not a replacement when the task specifically asks for a directory.

Older Windows VBA examples sometimes use the Shell BrowseForFolder method or Windows API calls. Those approaches can be more platform-specific and may require extra declarations or compatibility work. For a normal Excel macro, Application.FileDialog(msoFileDialogFolderPicker) is usually easier to read and maintain because it expresses the desired dialog type directly.

If the workbook is used in Excel for the web, a VBA folder picker is not an option because VBA does not run in the browser. The workflow must instead use a supported web-based tool or have users run the macro in desktop Excel. Which alternative fits depends on whether the task needs interactive folder selection, automated file access, or both.

Common problems and safe handling

  • The dialog opens, but Cancel causes an error: Check the return value from .Show before reading .SelectedItems(1), as in the example.
  • The macro cannot find expected files: Confirm that the selected directory is the right one, then check its contents and the file-name or extension rules used by your code.
  • A path works on one computer but not another: Look for hard-coded drive letters, user names, or assumptions about synchronized folders. Let the user choose the location or derive it from a validated setting.
  • The macro does not run: Confirm the workbook is saved in a macro-enabled format, opened in desktop Excel, and permitted to run macros under the organization’s security settings.
  • The selected folder is inaccessible: A dialog selection does not bypass file-system permissions. Handle errors around later file operations and explain which location the user needs access to.

For macros that process many files, consider reporting what will be processed before making changes. A confirmation prompt listing the chosen directory can catch accidental selections. If the code writes, moves, or deletes files, build in a clear confirmation step and test with sample data rather than valuable originals.

Frequently Asked Questions

What VBA command opens a dialog to select a folder?
Use Application.FileDialog(msoFileDialogFolderPicker). Set any dialog properties you need, call .Show, and read the selected directory from .SelectedItems(1) only after confirming the user did not cancel.

How do I get the folder path from the dialog?
After .Show returns -1, assign .SelectedItems(1) to a String variable. That value is the selected directory path; it is not the name of a file inside the directory.

How can I let the user select multiple folders?
The basic folder-picker workflow is designed around selecting a folder, and the example explicitly sets .AllowMultiSelect to False. If a workflow truly needs multiple directories, confirm the behavior supported by the specific Office version and design an alternative selection process rather than assuming this setting will provide multiple folder results.

Can I use a folder picker in Excel for the web?
No. Excel for the web does not execute VBA. Open the macro-enabled workbook in desktop Excel to run the macro, or redesign the workflow using a web-compatible tool.

Does selecting a folder open or change its files?
No. The dialog only returns a path. Your macro must separately open, read, or modify files, and those operations remain subject to file permissions and the code’s 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.