Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 是否将多个文件从多个文件夹复制到指定文件夹?_Excel_Vba - Fatal编程技术网

Excel 是否将多个文件从多个文件夹复制到指定文件夹?

Excel 是否将多个文件从多个文件夹复制到指定文件夹?,excel,vba,Excel,Vba,我需要将20多个文件复制到一个文件夹中,每个文件由唯一的文件夹存放 我创建了下面的代码 我明白了 编译错误:需要对象 文件夹名是报告的日期,例如090118,因此,我决定使用循环直到931月底。我还添加了错误处理代码以跳过假期和周末 Sub CopyFiles() Dim NewFolder As String Dim NDay As Long Dim FileName As String Dim Month As Variant Set Month

我需要将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

这可能不是最有效的方法,但是您可以使用循环选择文件,然后使用copyfile移动它


每个文件夹是否只包含要复制的文件每个文件夹包含多个文件,但我只需要复制一个特定文件。文件名有一个共同的因素,如果这有帮助的话。例如:C:\Folder\u 01\Balance\u Sheet\u 01 C:\Folder\u 02\Balance\u Sheet\u 02。C:\Folder\u 20\Balance\u Sheet\u 20仅供参考,startFolder=RangeCellsi,2,Cellsi,2可以替换为startFolder=Cellsi,2.value谢谢,@Sam!你能评论一下我编辑的问题吗?我试着使用Turtle的答案,但目前它不太符合我的要求。我创建了一个代码,但不幸的是,它无法运行。为什么要使用“设置月份”?删除“Set”并离开:“Month=InputBoxEnter Month,例如1月1日”,我再也没有收到任何错误。
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