How to Open the File Dialog Default Folder with Excel VBA – 3 Methods

Method 1 – Using the VBA FileDialog Property to Open the File Dialog Default Folder

Steps:

  • Press ALT+F11 to open the VBA Module window. You can also go to the Developer tab →  select Visual Basic.
  • In the Insert tab → select Module.

Excel VBA Open File Dialog Default Folder

  • Enter the following code in the Module window.
Option Explicit
Sub Open_Default_File_Dialog_1()
Application.FileDialog(msoFileDialogOpen).Show
End Sub

Excel VBA Open File Dialog Default Folder 2

VBA Code Breakdown

  • Open_Default_File_Dialog_1” is the Sub procedure.
  • The msoFileDialogOpen data type is used inside the FileDialog property.
  • The Show method opens the default file dialog folder.

 

  • Save the Module, place the cursor inside the code and click Run.

Excel VBA Open File Dialog Default Folder 3

  • The File Open dialog box will be displayed.

Excel VBA Open File Dialog Default Folder 4

Read More: How to Browse for File Path Using Excel VBA


Method 2 – Applying the VBA GetOpenFilename Method

Steps:

  • Open the Module window.
  • Enter the following code.
Option Explicit
Sub Open_Default_File_Dialog_2()
Dim Show_File_Dialog As String
Show_File_Dialog = Application.GetOpenFilename(FileFilter:= _
"Excel files (*.xlsx*), *.xlsx*", _
Title:="Select an Excel File", MultiSelect:=True)
End Sub

Excel VBA Open File Dialog Default Folder 5

VBA Code Breakdown

  • Open_Default_File_Dialog_2” is the Sub procedure.
  • The variable type is defined.
  • The GetOpenFilename method is used to show the default folder window.
  • It has several parameters: sets the file type as xlsx; sets the title of the window, and allows multiple files to be selected.
  • Run the code.

The file dialog default folder will be displayed.

Output of Method 2

Read More: How to Open Workbook with Variable Name with Excel VBA


Method 3 – Setting and Opening the Default File Dialog Folder with Excel VBA

Steps:

  • Open the Module window.
  • Enter the following code.
Option Explicit
Sub Set_Default_File_Dialog_Folder()
Dim File_Dialog_Box As Office.FileDialog
Set File_Dialog_Box = Application.FileDialog(msoFileDialogFilePicker)
    With File_Dialog_Box
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xlsx?", 1
        .Title = "Select an Excel File"
        .AllowMultiSelect = False
        .InitialFileName = "C:\Users\Rafiul Haq\Desktop\Exceldemy\16\"
        .Show
    End With
End Sub

VBA Code 3

VBA Code Breakdown

  • Set_Default_File_Dialog_Folder” is the Sub procedure .
  • The variable type is defined.
  • The variable is set as the “Application.FileDialog” property and the data type as “msoFileDialogFilePicker”. It will ask the user to select a file.
  • In the With statement the default file dialog folder is set inside the InitialFileName property.
  • Using the Show method, it will open the changed default folder.
  • Run the code.
  • The default folder location changed.

Output of Method 3

Read More: How to Open Folder and Select File Using Excel VBA


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Rafiul Haq
Rafiul Haq

Rafiul Haq worked as an Excel and VBA Content Developer in Exceldemy for over two years and published almost 200 articles for the website. He is passionate about exploring new aspects of Excel and VBA. He received his Bachelor of Science in Mechanical and Production Engineering (MPE) from the Islamic University of Technology. Rafiul furthered his education by obtaining an MBA in Finance from the Institute of Business Administration (IBA) at the University of Dhaka. Apart from creating... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo