我需要将20多个文件复制到一个文件夹中,每个文件都由唯一的文件夹保存。
我创建了以下代码。
我明白了
编译错误:必须提供对象。
文件夹名称是报告的日期(例如090118),因此,我决定使用循环,直到月底(931)。我还添加了错误处理代码,以跳过假期和周末。
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
答案 0 :(得分:1)
这可能不是最有效的方法,但是您可以使用循环选择文件,然后使用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