EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA File & Directories Functions VBA FileDialog
 

VBA FileDialog

Madhuri Thakur
Article byMadhuri Thakur

VBA FileDialog

Excel VBA FileDialog

While working there may be some scenarios where we need to open some other workbooks or any other files either to import data or to take a reference. There is one method to provide a path of the file in our code in VBA which will open the file or the folder for us. But what if we don’t remember the path how can we open the file. We might have to search the path first and then again provide the path. VBA comes with a solution which is known as FileDialog.

 

 

FileDialog is a property in VBA which allows us to open a file or a folder from a path. Using this property a user doesn’t have to specify the path of the folder instead the user can select the path. Not only this, FileDialog has a total of four properties. They are also known as constants for this property. They are as follows:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  1. msoFileDialogFilePicker: This allows the user to select a file.
  2. msoFileDialogFolderPicker: This allows the user to select a folder.
  3. msoFileDialogOpen: This allows the user to open a file.
  4. msoFileDialogSaveAs: This allows the user to save a file.

The method of using this object in VBA is as follows.

Application.FIledialog (Filedialog Type)

FileDialog type can be any one of the above four types. Now let us use this object in examples to learn about this object property more clearly.

How to Use Excel VBA FileDialog?

Below are the different examples to use FileDialog in Excel using VBA code.

You can download this VBA FileDialog Excel Template here – VBA FileDialog Excel Template

Excel VBA FileDialog – Example #1

First, let us help the user to select a file from a destination. In this example, the user will select a file from the folder and we will display the path for the file selected.

Follow the below steps to use excel VBA FileDialog:

Step 1: Go to the Developers tab and click on Visual Basic.

VBA FileDialog Example 1-1

Step 2: Open a Module from the Insert menu option as shown below.

VBA FileDialog Example 1-2

Step 3: Start the subprocedure to start working on example.

Code:

Sub SelectFile()

End Sub

VBA FileDialog Example 1-3

Step 4: Declare a variable as Filedialog as shown below.

Code:

Sub SelectFile()

Dim File As FileDialog

End Sub

VBA FileDialog Example 1-4

Step 5: Now let us use this object from the Application.Filedialog method as follows.

VBA FileDialog Example 1-5

Step 6: Once we open the parenthesis we can see the option for four FileDialog types as shown in the image below.

VBA FileDialog Example 1-6

Step 7: Select the option of Filedialog type as msoFileDialogFilePicker

Code:

Sub SelectFile()

Dim File As FileDialog
Set File = Application.FileDialog(msoFileDialogFilePicker)

End Sub

VBA FileDialog Example 1-7

Step 8: Now we need to make the dialog box for this example, let us use With Statement as follows.

Code:

Sub SelectFile()

Dim File As FileDialog
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File

End With

End Sub

VBA FileDialog Example 1-8

Step 9: If there are any filters we need to clear the filters, put a dot statement and write the statement as shown below.

Code:

Sub SelectFile()

Dim File As FileDialog
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Filters.Clear
End With

End Sub

VBA FileDialog Example 1-9

Step 10: Now do we want the user to select multiple files at once or a single file? For this example let us stick with the single file at a time, so use .Allowmultiselect as false.

Code:

Sub SelectFile()

Dim File As FileDialog
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Filters.Clear
.AllowMultiSelect = False
End With

End Sub

VBA FileDialog Example 1-10

Step 11: Now in order to show the dialog box we can write .show to show the dialog box as follows.

Code:

Sub SelectFile()

Dim File As FileDialog
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Filters.Clear
.AllowMultiSelect = False
.Show
End With

End Sub

VBA FileDialog Example 1-11

Step 12: Once we execute the above code we can see the dialog box as follows.

VBA FileDialog Example 1-12

Step 13: Since we need to display the user path selected, declare another variable as a string in the code.

Code:

Sub SelectFile()

Dim File As FileDialog
Dim Path As String
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Filters.Clear
.AllowMultiSelect = False
.Show
End With

End Sub

VBA FileDialog Example 1-13

Step 14: We will use a selecteditems statement to find the path for the of the file user selected and store it in our variable as follows.

Code:

Sub SelectFile()

Dim File As FileDialog
Dim Path As String
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Filters.Clear
.AllowMultiSelect = False
.Show
Path = .SelectedItems(1)
End With

End Sub

VBA FileDialog Example 1-14

Step 15: After the With Statement use the msgbox function to display the address of the file selected.

Code:

Sub SelectFile()

Dim File As FileDialog
Dim Path As String
Set File = Application.FileDialog(msoFileDialogFilePicker)
With File
.Filters.Clear
.AllowMultiSelect = False
.Show
Path = .SelectedItems(1)
End With
MsgBox Path

End Sub

VBA FileDialog Example 1-15

Step 16: Let us execute the above code and select a file and press ok to see the path as follows.

File Path Example 1-16

Excel VBA FileDialog – Example #2

Now let us see another option of the VBA FileDialog object which is msoFileDialogSaveAs. This property is used to save a file on any path. Follow the below steps to use excel VBA FileDialog.

Step 1: In the same module let us write another sub procedure to save the file as follows.

Code:

Sub SaveFile()

End Sub

VBA FileDialog Example 2-1

Step 2: Now declare two variables one as string while another as an integer.

Code:

Sub SaveFile()

Dim Choice As Integer, Path As String

End Sub

VBA FileDialog Example 2-2

Step 3: Now let us make the dialog box visible to the user using the show property as follows.

Code:

Sub SaveFile()

Dim Choice As Integer, Path As String
Choice = Application.FileDialog(msoFileDialogSaveAs).Show

End Sub

Show property Example 2-3

Step 4: Now use IF statement to determine the choice of the user as follows.

Code:

Sub SaveFile()

Dim Choice As Integer, Path As String
Choice = Application.FileDialog(msoFileDialogSaveAs).Show
If Choice <> 0 Then

End Sub

IF Statement Example 2-4

Step 5: Now let us get the path selected by the user in our path variable as follows.

Code:

Sub SaveFile()

Dim Choice As Integer, Path As String
Choice = Application.FileDialog(msoFileDialogSaveAs).Show
If Choice <> 0 Then
Path = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)

End Sub

Get the path selected Example 2-5

Step 6: Now let us display the result using the msgbox function and end the IF Loop.

Code:

Sub SaveFile()

Dim Choice As Integer, Path As String
Choice = Application.FileDialog(msoFileDialogSaveAs).Show
If Choice <> 0 Then
Path = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
MsgBox Path
End If

End Sub

Using msgbox function Example 2-6

Step 7: Now execute the above code and see the result, it opens a dialog box to save the file.

File Save Dialog Box Example 2-7

Step 8: Give a file name and press Save it will give us the Path for the file as follows.

File Path Example 2-8

Things to Remember

  • FileDialog is an object property of VBA.
  • We do not need to specify the path to code using this object.
  • Using the dot operator we can see and use the IntelliSense properties of the object.

Recommended Articles

This is a guide to VBA FileDialog. Here we discuss how to use FileDialog object in Excel using VBA code along with some practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Check File Exists
  2. VBA Delete File
  3. VBA Code
  4. VBA Get Cell Value

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA FileDialog Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW