我正在尝试将文件从各种文件夹(excel文件)复制到一个文件夹。我希望它从那些文件夹中复制excel文件,但是该文件夹可能存在或可能不存在,并且如果该文件夹不存在,希望能够恢复到下一个错误。 VBA新手。下面本质上是我想要的,但是循环不起作用,我希望能够按以下扩展名搜索最后10个文件夹,如果不存在,则继续进行下一个,依此类推,直到找到它为止到达10个文件夹名称,并复制该文件夹中的所有excel文件
Sub Day2()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim i As Integer
For i = 1 To 10
FromPath = "Sample folder\Sample - Submited to TD by Date\" & Format((Now - i), "yyyy") & "\" & Format((Now - i), "yyyy") & "_" & Format((Now - i), "mm") & "\" & _
"Submitted Requests - " & Format((Now - i), "mm") & "-" & Format((Now - i), "dd") & "-" & Format((Now - i), "yy")
ToPath = "\Sample\To Combine\"
FileExt = "*.xl*"
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject("scripting.filesystemobject")
FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
Next i
End Sub