Selecting a file using the FileDialog

VBA Code Snippets

In a Macro or Add-in there may be times when you require the user to select a file.  It is possible to use the standard FileDialog box for this purpose.  The following code is intended to provide the most common settings which you can amend to meet your needs.

Open the file DialogBox for selecting a file

Sub selectFile()

Dim dialogBox As FileDialog
Set dialogBox = Application.FileDialog(msoFileDialogOpen)

'Set the display properties - these are optional
'All the settings must be applied before the .Show command

'Do not allow multiple files to be selected
dialogBox.AllowMultiSelect = False

'Set the title of of the DialogBox
dialogBox.Title = "Select a file"

'Show the dialog box and output full file path and file name
If dialogBox.Show = -1 Then
   MsgBox "You selected: " & dialogBox.SelectedItems(1)
End If

End Sub

Selecting multiple files

If you the user is allowed to select more than one file the following code can be used:

Sub selectFiles()

Dim dialogBox As FileDialog
Set dialogBox = Application.FileDialog(msoFileDialogOpen)
Dim i As Integer
'Set the display properties - these are optional
'All the settings must be applied before the .Show command

'Allow multiple files to be selected
dialogBox.AllowMultiSelect = True

'Set the title of of the DialogBox
dialogBox.Title = "Select a file"

'Show the dialog box
'Loop through each selected item and output to Immediate Window
For i = 1 To dialogBox.SelectedItems.Count
    Debug.Print dialogBox.SelectedItems(i)
Next i

End Sub
100 Excel VBA Macros

Do you know the fastest way to learn foreign languages?  It is to read, write, speak, and think in that language as often as possible.  Apart from speaking, programming languages are no different.  The more you immerse yourself in that language, the faster you will pick it up.

100 Excel Macros Book

Therefore, what most people like you need is lots of examples that you can practice.  That is why the 100 Excel VBA Macros eBook exists.  It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers.  The book contains:

  • 100 example codes to practice reading and writing macros that will embed the language into your thinking.
  • An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
  • Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
  • Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.

Setting the default folder options

'Set the default folder to open
dialogBox.InitialFileName = "C:\Users\marks\Documents"

'Set filters to show specific files types only - these are optional
'Clear any existing filters first
'Add more filters - use ; to separate filters for the same name
dialogBox.Filters.Add "Excel workboooks", "*.xlsx;*.xls;*.xlsm"

File name or full file path

Sometimes you want the full file path of the selected item

'Show the full file path (folder & file name)
MgsBox dialogBox.SelectedItems(i)

Other times, you just want the name of the file (not the folder)

'Just show the file name
MsgBox Dir(dialogBox.SelectedItems(1))

Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Email Address * First Name *

By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.

Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet your situation.  We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs.  By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.

But, if you’re still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community.  Remember, the people on these forums are generally giving their time for free.  So take care to craft your question, make sure it’s clear and concise.  List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

2 thoughts on “Selecting a file using the FileDialog

  1. black pepper says:

    How do you open a single specific file with complete pathname to include filename already known using msoFileDialogOpen? No selection by the user, just open it immediately.

Leave a Reply

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