我正在使用宏将CSV文件合并为一个。 宏工作正常,但我有一个块,允许选择一个包含CSV文件的文件夹进行合并。
两个问题:
代码块:
'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
现在可以从以下位置选择一个文件夹:
我希望更多选择:
答案 0 :(得分:1)
您可以使用Application.FileDialog
。这允许浏览文件夹并选择文件。
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
答案 1 :(得分:1)
根本无法选择多个文件夹。即使同时选择两个文件夹也行不通。
您可以选择...
以下是选择一个文件夹的方法:
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