我使用下一个代码来从某个路径选择一个文件夹并导入其中的所有文件:
Function GetFolder()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.Title = "Select Excel Workbook(s) Folder"
Dim vrtSelectedItem As Variant
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
GetFolder = vrtSelectedItem
Next vrtSelectedItem
Else
End If
End With
Set fd = Nothing
End Function
当文件夹选取器窗口打开时,它会在桌面上启动。有没有办法让它在打开时走特定的路径?或者打开excel文件本身所在的位置?
您将更新 InitialFileName 属性,并且可以将其设置为使用 ActiveWorkbook.Path
您需要确保包含结束斜杠,否则它只会显示上一个文件夹,而不是您想要的文件夹。
此外,没有理由循环遍历 .SelectedItems 集合,因为FolderPicker FileDialog 不支持多重选择。
总之,我认为这是您正在寻找的代码:
Function GetFolder()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & Application.PathSeparator
.Title = "Select Excel Workbook(s) Folder"
If .Show = True Then
GetFolder = .SelectedItems(1)
Else
GetFolder = False
End If
End With
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)