I am trying to write a VBA code to consolidate Excel files from different folders to a single folder. Within each folder there is a single Excel, that I would like to move it to a single, compiled folder. Here is my code so far:
Sub move_data()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim Fdate As Date
Dim FileInFromFolder As Object
MkDir "C:\User\TEST\"
FromPath = "C:\User\MainFolder\"
ToPath = "C:\User\TEST\"
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
For Each FileInFromFolder In FSO.GetFolder(FromPath).Files
FileInFromFolder.Move ToPath
Next FileInFromFolder
End Sub
The code is incomplete. It is unable to get the files from the subfolder within the folder (as shown in the image). May I seek advice from you all on how I can improve this code further?
The area I am looking to change is 'FromPath', if it is possible to include a wildcard to specify the subfolders?
Hope to hear from you all soon. Thank you in advance!