How to pick a file to load In VBA

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

Sub PickFile_FileDialog()
Dim fd As FileDialog
Dim selectedPath As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
.Title = “Select a file to open”
.AllowMultiSelect = False
If .Show = –1 Then
selectedPath = .SelectedItems(1)
MsgBox “You selected: “ & selectedPath
Else
MsgBox “No file was selected.”
End If
End With
End Sub

This displays a standard Windows file picker dialog.

Customizing FileDialog Options

  • Set default folder:

    .InitialFileName = "C:\MyFolder\"
  • Add file type filters:

    .Filters.Clear
    .Filters.Add "Excel Files", "*.xlsx;*.xls"
    .Filters.Add "CSV Files", "*.csv"
  • Allow multiple selection:

    .AllowMultiSelect = True

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

Sub PickFile_GetOpenFilename()
Dim selectedPath As Variant
selectedPath = Application.GetOpenFilename( _
FileFilter:=“Excel Files (*.xlsx), *.xlsx; *.xlsm”, _
Title:=“Select a file to open”)

If selectedPath <> False Then
MsgBox “You selected: “ & selectedPath
Else
MsgBox “No file was selected.”
End If
End Sub

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 is False).

  • Error handling
    Use On Error Resume Next around risky file operations.

  • Returning multiple selected paths
    Only possible with FileDialog. Use .AllowMultiSelect = True and 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.




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

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:
Tags: , , ,
Previous Post

How To Parse A Flat File In Excel VBA

Next Post

What is the purpose of the Me keyword in Access VBA?

Leave a Reply

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