我目前在代码上苦苦挣扎。我希望有人能帮助我。 下面我调整了一个代码(来自互联网),所以我得到了一个集合中的文件夹和子文件夹中的所有文件。 我想复制每个csv文件的内容并将其粘贴到主Excel文件中。由于我是VBA的新手,我不知道如何处理这个问题。
Public Sub NonRecursiveMethod()
Dim fso, oFolder, oSubfolder, oFile, queue As Collection
Dim oApp As Object
Dim NameFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set queue = New Collection
Set oApp = CreateObject("Shell.Application")
Set NameFolder = oApp.BrowseForFolder(0, "Select folder with CSV files", 512)
queue.Add fso.GetFolder(NameFolder.Self.Path)
Do While queue.Count > 0
Set oFolder = queue(1)
queue.Remove 1 'dequeue
'...insert any folder processing code here...
MsgBox "Folder = " & oFolder
For Each oSubfolder In oFolder.SubFolders
queue.Add oSubfolder 'enqueue
Next oSubfolder
For Each oFile In oFolder.Files
'...insert any file processing code here...
MsgBox "File = " & oFile
Next oFile
Loop
End Sub