【问题标题】:Importing folder to Excel (FileDialogFolderPicker) using VBA使用 VBA 将文件夹导入 Excel (FileDialogFolderPicker)
【发布时间】:2013-08-08 21:32:50
【问题描述】:

我正在使用下一个代码从某个路径中选择一个文件夹并导入其中的所有文件:

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文件本身所在的位置?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您将更新 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
    

    【讨论】:

      【解决方案2】:

      .Show之前添加这样一行:

       fd.InitialFileName = "c:\whateverInitialDirectory"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-19
        • 2016-02-05
        • 2015-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-06
        • 1970-01-01
        相关资源
        最近更新 更多