How to Pick a File in VBA: FileDialog & GetOpenFilename Explained
When building Excel VBA applications, you’ll often need to let users pick a file to load in VBA. Instead of hard-coding file paths, you can use built-in dialogs that make file selection easy and user-friendly.
VBA offers two main approaches:
-
FileDialog object (flexible, customizable)
-
GetOpenFilename method (simple, quick)
This guide shows how to use both, with code examples and best practices.
Choosing the Right VBA File Picker
When deciding between FileDialog and GetOpenFilename, consider:
-
FileDialog – Best if you need filters, default folders, or multiple file selection.
-
GetOpenFilename – Best if you just need a simple file path, with minimal setup.
Using the FileDialog Object in VBA
The FileDialog object provides the most control when asking users to pick a file in VBA.
Example: Basic FileDialog Code
This displays a standard Windows file picker dialog.
Customizing FileDialog Options
-
Set default folder:
-
Add file type filters:
-
Allow multiple selection:
Using the GetOpenFilename Method in VBA
The GetOpenFilename method is a lightweight alternative to FileDialog. It doesn’t require an object, and it’s great for quick scripts.
Example: Basic GetOpenFilename Code
Pros and Cons of GetOpenFilename
✅ Very simple to use
✅ Requires no setup
❌ Limited customization (no multi-select, no custom dialog titles beyond basics)
Advanced Tips for File Selection in VBA
-
Handle user cancel actions
Always check if the user pressed Cancel (return value isFalse). -
Error handling
UseOn Error Resume Nextaround risky file operations. -
Returning multiple selected paths
Only possible with FileDialog. Use.AllowMultiSelect = Trueand loop through.SelectedItems.
Best Practices for User-Friendly File Pickers
-
Use a clear dialog title (e.g., “Select the data file to import”).
-
Pre-set the folder to where users are likely to look.
-
Restrict file types with filters so users don’t accidentally pick the wrong file.
Summary
To let users pick a file in VBA:
-
Use FileDialog if you need flexibility (filters, default folders, multiple files).
-
Use GetOpenFilename if you want something lightweight and quick.
Both methods make your VBA applications more professional and user-friendly.
———————————————————
More Information:
Picking a file to load in your Microsoft App is a very important skill to know.
In this blog post you will see how to do it.
First you need to set a reference to the MS office object library
From VBE editor –> select Tools > MS office object library (click check mark)

Sub SelectFile()
strFileName = GetFile(strStart, "Select Your Import File")
MsgBox strFileName
End Sub
Public Function GetFile(start_here, title_bar As String) As String
Dim intChoice As Integer
Dim strPath As String
'needs reference to the MS office object library
'only allow the user to select one file
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.InitialFileName = start_here
.Title = title_bar
End With
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
'MsgBox strPath
GetFile = strPath
End If
End Function
Live Sample:
Stay tuned, because I’m going to show you how to use this in some examples.
Let me know if you have any questions.
How to Fix Run Time Error 1004 in Excel
If you work with Microsoft Excel frequently, chances ar ling for a solution. Fortunately, this error is well-documented, and there are several ways to resolve it. In this article, we’ll explore the causes of run time error 1004, practical steps to fix it, and preventive measures to reduce the chances of it happening again. What […]
How To Make An Access Form Time Picker
Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]
How To Parse A Flat File In Excel VBA
In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]
What is the purpose of the Me keyword in Access VBA?
What does the Me keyword mean? “Me” refers to the Access form currently in focus. Instead of writing out the entire form reference, you can just use the keyword “Me” which is easier. Like: Me.txtbox = “I am a textbox on the form that currently has the focus.” or you can update a label’s caption […]
Support these sponsors:
