0

I need to copy 20+ files, each housed by unique folders, into a single folder.

I created the below code.

I get

Compile Error: Object required.

Folder name is the date of the report (eg. 090118), hence, I decided to use a loop until the end of the month (931). I also added error handling code to skip the holidays and weekends.

Sub CopyFiles()

    Dim NewFolder As String
    Dim NDay As Long
    Dim FileName As String
    Dim Month As Variant

    Set Month = InputBox("Enter month, eg. 01-January")
    NewFolder = "C:\Results\Trading\2018\" & Month & "\Backtest Daily Files\Daily GS\" 'Don't forget to edit this
    NDay = 901

    On Error Resume Next

    Do While NDay < 931

        FileName = Dir("C:\Reports\2018\" & Month & "\0" & NDay & "18\GS_Futures*.cs*")

        FileCopy FileName, NewFolder

        NDay = NDay + 1

    Loop

End Sub
2
  • Does each folder contain only file to be copied Commented Oct 8, 2018 at 6:12
  • Each folder contains several files but I only need to copy one specific file. There's a common factor when it comes to the file name, if this helps. For example: C:\Folder_01\Balance_Sheet_01 C:\Folder_02\Balance_Sheet_02 . . . C:\Folder_20\Balance_Sheet_20 Commented Oct 8, 2018 at 6:23

1 Answer 1

1

This might not be the most efficient way, however you could use a loop to select the file and then move it useing copyfile.

Sub Move_File()
Dim myFSO As Object
Dim startFolder As String, endFolder As String
Dim startFile As String
startFile = "test.xls"

For i = 1 To 20
startFolder = Range(Cells(i,2),Cells(i,2))
endFolder = "C:\Test\"
myFSO.copyfile startFolder & startFile, endFolder & endFile, True
Next i

End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Just an FYI, startFolder = Range(Cells(i,2),Cells(i,2)) can be replaced with startFolder = Cells(i, 2).value
Thank you, @Sam! Could you kindly comment on my edited question? I tried using Turtle's answer but it didn't quite fit with my requirements at the moment. I created a code but unfortunately, it won't run.
Why are you using 'Set Month'? Deleting the 'Set' and just leaving: 'Month = InputBox("Enter month, eg. 01-January")' I don't get any errors anymore.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.