使用VBA将文件夹导入Excel(FileDialogFolderPicker) [英] Importing folder to Excel (FileDialogFolderPicker) using VBA
问题描述
我正在使用下一个代码,以便从某个路径中选择一个文件夹并导入其中的所有文件:
I'm using the next code in order to select a folder from a certain path and import all the files inside it:
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文件本身所在的位置?
When the Folder Picker window opens it start on the desktop. Is there a way to make it go to a specific path upon opening? or open where the excel file itself is located?
推荐答案
您将更新InitialFileName属性,并将其设置为使用ActiveWorkbook.Path.您需要确保包含结尾斜杠,否则它将仅显示前一个文件夹,而不显示所需的文件夹.同样,没有理由遍历.SelectedItems集合,因为FolderPicker FileDialog不支持多重选择.
You would update the InitialFileName property, and you could set it to use the ActiveWorkbook.Path You'll need to make sure that you include the ending slash, or it will only display the previous folder instead of the folder you want. Also, there is no reason to loop through the .SelectedItems collection because the FolderPicker FileDialog doesn't support mutliple selections.
总而言之,我认为这是您正在寻找的代码:
In summary, I think this is the code you're looking for:
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
这篇关于使用VBA将文件夹导入Excel(FileDialogFolderPicker)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!