0

I'm trying to copy files from varies folders(excel files) to one folder. I want it to copy the excel files from those folders, but the folder may or may not exsist and would like to be able to resume to the next on error if the folder doesn't exist. new to vba. Below is essentially what I would like but the loop doesn't work and I want to be able to just search the last 10 folder by the extensions below and if it doesn't exist to go on to the next one and so on until it reaches 10 folders names and copies all the excel files from that folder

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
doe
  • 9
  • 2
  • https://stackoverflow.com/questions/22645347/loop-through-all-subfolders-using-vba – Marcucciboy2 Sep 26 '18 at 20:05
  • When the first folder is found does it stop there, or continue with any other existing ones? – Tim Williams Sep 26 '18 at 20:46
  • 1
    Possible duplicate of [Excel VBA Check if directory exists error](https://stackoverflow.com/questions/15480389/excel-vba-check-if-directory-exists-error) – BruceWayne Sep 26 '18 at 21:29
  • 1
    There are tons of examples on this site as well as Microsoft. I think you can quickly find your answer with a little more research – Jamie Riis Sep 26 '18 at 22:44

0 Answers0