Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Open Windows Explorer and Select a File

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To open Windows Explorer with a file already selected from Excel VBA, use the Application.FileDialog(msoFileDialogFilePicker) object and call its Show method. The user picks a file in the standard Windows dialog, and your code reads the full path from SelectedItems(1). This is the reliable, native way to let a user choose a file without hard-coding paths. You need a Windows PC with Excel (any modern desktop version), the VBA editor open (Alt+F11), and a macro-enabled workbook (.xlsm) if you want to keep the code. The steps below use the FileDialog approach because it works across Excel versions and avoids fragile Shell calls.

What You'll Need

  • Excel for Windows (the FileDialog object is not available in Excel for Mac in the same form).
  • A macro-enabled workbook saved as .xlsm, or a workbook you will save that way before closing.
  • Access to the VBA editor: press Alt+F11, or enable the Developer tab via File > Options > Customize Ribbon.
  • Basic familiarity with inserting a module and running a macro (Alt+F8).

Step-by-Step

  1. Open the VBA editor. In Excel, press Alt+F11. If nothing happens, enable the Developer tab first: File > Options > Customize Ribbon, tick Developer in the right-hand list, then click OK and use Developer > Visual Basic. Menu labels can differ slightly by Excel version and region.
  2. Insert a standard module. In the VBA editor, right-click your workbook name in the Project Explorer, choose Insert > Module. A blank code window appears. Code placed in a sheet module also works, but a standard module keeps it reusable.
  3. Paste the core macro. Copy this into the module:
    Sub PickAFile()
        Dim fd As FileDialog
        Dim chosen As String
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .Title = "Select a file"
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "Excel files", "*.xls; *.xlsx; *.xlsm"
            .Filters.Add "All files", "*.*"
            If .Show = -1 Then
                chosen = .SelectedItems(1)
                MsgBox "You selected: " & chosen
            Else
                MsgBox "No file selected."
            End If
        End With
        Set fd = Nothing
    End Sub
  4. Run it. Place the cursor inside the subroutine and press F5, or go back to Excel and use Developer > Macros (Alt+F8), select PickAFile, and click Run. The standard Windows file picker opens.
  5. Select a file and confirm. Browse to a folder, click a file once, and click Open. The message box shows the full path, which is what you would pass to Workbooks.Open or any other file-handling code.
  6. Use the path in your own code. Replace the MsgBox line with the action you need, for example Workbooks.Open chosen. Keep the If .Show = -1 check so the macro exits cleanly when the user clicks Cancel.
  7. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A normal .xlsx file discards the macro when saved.

Common Mistakes

  • Forgetting the -1 check. Show returns -1 when the user picks a file and 0 when they cancel. Reading SelectedItems(1) after a cancel throws an error because the collection is empty.
  • Using Application.GetOpenFilename and expecting Explorer. That function shows a similar dialog but returns a string (or False), and it does not select a file in an Explorer window. It is fine for choosing a file, but it is a different tool.
  • Assuming Mac parity. The FileDialog object exists on Mac Excel but behaves differently and some filter options are limited. If you need cross-platform code, test on both, or branch with #If Mac Then.
  • Hard-coding a path with Shell "explorer.exe /select,...". This opens Explorer with a file highlighted, but the path must already be known and correctly quoted. It is not a file picker and fails silently if the path contains errors.
  • Saving as .xlsx. The macro disappears on close, and users think the code was deleted.
  • Leaving Set fd = Nothing out. Not fatal, but releasing the object is good practice in longer macros that run repeatedly.

Tips

  • Set .InitialFileName to a default folder or file so the dialog opens where users expect, for example .InitialFileName = "C:\Reports\".
  • Use .AllowMultiSelect = True and loop through .SelectedItems when users need to pick several files.
  • Add filters in the order users are most likely to need them; the first filter is selected by default.
  • Store the chosen path in a module-level variable if another macro needs it later in the same session.
  • If you genuinely want Explorer to open with a file highlighted rather than a picker, use Shell "explorer.exe /select," & Chr(34) & fullPath & Chr(34), vbNormalFocus only when the path is already known and valid.

Troubleshooting

  • Compile error: user-defined type not defined. The FileDialog type is unavailable, usually on Mac Excel or a very old version. Use Application.GetOpenFilename as a fallback, or declare the variable as Object.
  • Macro does nothing. Macros may be disabled. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, and enable macros for the session. Also confirm you are running the macro from the correct workbook.
  • Runtime error 5 or 9 after Cancel. The code tried to read SelectedItems(1) when nothing was selected. Add or restore the If .Show = -1 guard.
  • Dialog opens in an unexpected folder. Clear or set .InitialFileName; the dialog otherwise remembers the last folder used.
  • File opens read-only or prompts for a password. That is the file's own protection, not a VBA fault. Open it manually to confirm, then handle it in code or choose another file.
  • Explorer does not come to the front. If you used a Shell call, the window may open behind Excel. Bring it forward manually; the FileDialog picker normally appears on top by itself.

Frequently Asked Questions

Can VBA open Windows Explorer with a file already selected?

Yes, but not with FileDialog. Use Shell "explorer.exe /select," & Chr(34) & fullPath & Chr(34) with a valid, known path. FileDialog is for choosing a file, not for highlighting one in an existing Explorer window.

What is the difference between FileDialog and GetOpenFilename?

FileDialog gives you an object with filters, multi-select and a title you can set. GetOpenFilename returns a string path or False and is simpler but less configurable. Both show a file picker; neither opens a full Explorer window.

Why does my macro fail when the user clicks Cancel?

Because SelectedItems is empty after a cancel. Check that .Show = -1 before reading SelectedItems(1), and show a message or exit the sub otherwise.

Does this work in Excel for Mac?

The FileDialog object is available but behaves differently, and some filter settings are limited. Test on Mac before distributing, or use GetOpenFilename as a simpler cross-platform option.

Can I let the user pick more than one file?

Yes. Set .AllowMultiSelect = True and loop from 1 to .SelectedItems.Count, collecting each path into an array or a worksheet column.

How do I open the chosen file automatically?

After storing the path, call Workbooks.Open chosen for Excel files, or use Shell "explorer.exe " & Chr(34) & chosen & Chr(34) to open it with its default program.