Vba file dialog in Excel
Folder picker is one of the four objects of a dialog box control in Excel vba.
To use a folder picker, the "msoFileDialogFolderPicker" has to be referenced.
This dialog box is very useful, when there is a need to select a folder in the application.
For example, consider a situation, when a set of excel files has to be manipulated programatically.
In such a case, the user will be opted to choose the folder containing the excel files, here is where the folder dialog comes to the rescue.
The following example illustrates the folderpicker.
- Sub folderpkr()
- Dim fldr As FileDialog
- Dim sItem As String
- Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
- With fldr
- .Title = "Select a Folder"
- .AllowMultiSelect = False
- .InitialFileName = Application.DefaultFilePath
- If .Show <> -1 Then GoTo NextCode
- sItem = .SelectedItems(1)
- End With
- MsgBox "You have selected " & sItem & " folder"
- GetFolder = sItem
- Set fldr = Nothing
- End Sub
The above example is a simple illustration of this dialog box that displays the selected folder to the user.
The Screenshot of the code and its outputs are as shown below: