The FileDialog object was introduced in Excel 2002.Therefore,this technique will not work with earlier versions of Excel.
The following procedure displays a dialog box, which allows the user to select a directory. The selected directory name (or Canceled) is then displayed by using the MsgBox function.
Sub GetAFolder2() ' For Excel 2002 and later
With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = Application.DefaultFilePath & "\" .Title = "Please select a location for the backup" .Show
If .SelectedItems.Count = 0 Then MsgBox "Canceled"
MsgBox .SelectedItems(l) End If End With End Sub
The FileDialog object lets you specify the starting directory by specifying a value for the InitialFileName property. In this case, the code uses Excel's default file path as the starting directory.
Was this article helpful?