Open Explorer and select file in Excel VBA

How to: open an Explorer window so a user can select a file in Excel VBA.

About

The below VBA function whenc alled will open an Explorer window (when using windows) and allow the user to navigate through the file system to select a file. The function will return a single string containing the full path and file name.

Code

Below is the VBA code which can serve as a starting point for further development.

Explanation

First we open an Explorer window with the Application.FileDialog function using the msoFileDialogFilePicker option so the user must select a file rather than a folder. Then we clear filters (so in theory a user can select any file type), we do not allow multiple selections and if the user selects a file we return the path and name to the fileNamePath variable.

Links

  • FileDialog object reference link
  • MsoFileDialogType Enum reference link

About the author

Leave a Reply

Your email address will not be published. Required fields are marked *