代码1:(Function)
Public Function ChooseFolder() As String
'定义并新建一个对话框对象
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
'如果当前没有对话框显示,就让他弹出对话框
If dlgOpen.Show = -1 Then ChooseFolder = dlgOpen.SelectedItems(1)
MsgBox (ChooseFolder) '弹出我们选择的文件夹
Set dlgOpen = Nothing
End Function
代码2:(宏程序)
Sub FolderPicker()
'定义并新建一个对话框对象
Dim FolderDialogObject As FileDialog
Set FolderDialogObject = Application.FileDialog(msoFileDialogFolderPicker)
'配置对话框,默认路径为C盘
With FolderDialogObject
.Title = "请选择要查找的文件夹"
.InitialFileName = "C:\"
End With
'显示对话框
FolderDialogObject.Show
'获取选择对话框选择的文件夹
paths = FolderDialogObject.SelectedItems(1)
MsgBox (paths) '弹出我们选择的文件夹
End Sub