1. Folder
Dim objFD As Object
Dim strOut As String
strOut = vbNullString
'msoFileDialogFolderPicker = 4
Set objFD = Application.FileDialog(4)
If objFD.Show = -1 Then
strOut = objFD.SelectedItems(1)
End If
Set objFD = Nothing
FolderSelection = strOut
Dim strChoice As String
strChoice = FolderSelection
If Len(strChoice) > 0 Then
Me.Text1 = strChoice
Else
' what should happen if user cancelled selection?
End If
2. File
Dim objDialog As Object
Set objDialog = Application.FileDialog(3)
With objDialog
.AllowMultiSelect = False
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
'Me.Text1 = Dir(.SelectedItems(1))
Me.Text1 = .SelectedItems(1)
End If
End With
Set objDialog = Nothing
本文介绍使用VBA在Excel中实现文件和文件夹的选择功能。通过两种不同类型的文件对话框,用户可以轻松地选取所需的文件或文件夹。代码详细展示了如何设置对话框属性及处理用户的选择。
5873

被折叠的 条评论
为什么被折叠?



