select多个文件夹 – VBA

我正在使用macros将CSV文件合并成一个。 macros运行良好,但我有一个块,允许select一个文件夹与CSV文件合并。

两个问题:

  1. 我可以select多个文件夹进行合并吗? (在一个文件夹中,我有很多CSV文件)。
  2. A如何从其中select其他初始目录来select文件夹? 我需要从服务器使用文件夹。

代码块:

'Browse to the folder with CSV files Set oApp = CreateObject("Shell.Application") Set oFolder = oApp.BrowseForFolder(0, "Select folder with CSV files", 512) If Not oFolder Is Nothing Then foldername = oFolder.Self.Path If Right(foldername, 1) <> "\" Then foldername = foldername & "\" End If 

现在我的可用性从以下select一个文件夹:

没有足够的选择

我很想select更像这样的:

更多的选择

select多个文件夹是不可能的。 即使同时select两个文件夹也不行。

您可以select…

  • 一个文件夹中 多个文件 (请参阅@FunThomas答案 )
  • 或者只有一个文件夹。

以下是如何select一个文件夹:

 Public Sub SelectFolder() Dim fdl As FileDialog Set fdl = Application.FileDialog(msoFileDialogFolderPicker) With fdl .InitialFileName = "C:\Temp" 'where we start choosing a folder If .Show <> -1 Then MsgBox "canceled" 'nothing was selected Else Debug.Print .SelectedItems(1) 'the selected folder End If End With End Sub 

您可以使用Application.FileDialog 。 这允许浏览文件夹并select文件。

 Dim fileCount As Long ' Open the file dialog With Application.FileDialog(msoFileDialogOpen) .AllowMultiSelect = True .Filters.Clear .Filters.Add "CSV files", "*.csv" .Show For fileCount = 1 To .SelectedItems.Count Debug.print .SelectedItems(fileCount) Next fileCount End With