Excel VBA引用文件夹选取器* new到VBA *

时间:2018-04-11 19:53:05

标签: excel vba

我有一个拼凑在一起的代码。目标是将所有excel文件放在一个文件夹中,打开它们并将这些页面添加到一起以形成一个数据透视表。我可以很容易地设置一个path ='foldername'并且它可以工作,但是,我想要将它转到我可以只浏览文件夹的位置,并让路径引用来自msoFileDialogFolderPicker的所选路径。我知道path = Diafolder是不正确的,我正在寻找答案。 提前谢谢你。

Dim Diafolder As FileDialog
Dim path As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

' Open the file dialog
Set Diafolder = Application.FileDialog(msoFileDialogFolderPicker)
Diafolder.AllowMultiSelect = False
Diafolder.Show

Msgbox Diafolder.SelectedItems(1)

Set Diafolder = Nothing
path = Diafolder

fileName = Dir(path & "*.xls")

Do While fileName <> ""

1 个答案:

答案 0 :(得分:0)

这是你可以这样做的方式:

Set Diafolder = Application.FileDialog(msoFileDialogFolderPicker)
With Diafolder 

    .AllowMultiSelect = False
    .Title = "Select a folder..."

    If .Show = True Then
        path = .SelectedItems(1)
    End If

End With