Vba folder dialog in Excel
Filedialog is used to pick a file of any kind during the execution of the program.
This provides a file dialog box functionality similar to the functionality of the standard Open and Save dialog boxes found in Microsoft Office applications.
The�FileDialog�property is located in each individual Office application's�Application�object.
There are four types of Filedialog object:
1.Open
2.Save As
3.FilePicker
4.Folder Picker
In this chapter, File picker is explained.
As the name indicates, Filepicker is the one that allows the user to choose a file during the course of a program.
This situation arises in a number of occassions where a user has t select a file to work on.
In these times, the file picker is used.This displays a dialog box opting the user to choose a file of choice.
The follwing example illustrates this:
- Sub filedilog()
- Dim fDialog As FileDialog, result As Integer
- Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
- 'Optional: FileDialog properties
- fDialog.AllowMultiSelect = False
- fDialog.Title = "Select a file"
- fDialog.InitialFileName = "C:\"
- 'Optional: Add filters
- fDialog.Filters.Clear
- fDialog.Filters.Add "Excel files", "*.xlsx"
- fDialog.Filters.Add "All files", "*.*"
- 'Show the dialog. -1 means success!
- If fDialog.Show = -1 Then
- MsgBox fDialog.SelectedItems(1)
- End If
- End Sub
The above code opts the user to select a file and dispaye the same to the user in a messagebox.
The screenshot of the code and the output is as follows