将工作簿一张一张地复制到存档文件夹中

时间:2018-09-11 03:53:30

标签: excel vba excel-vba

我使用了下面的代码,该代码完美地完成了拾取所有文件并将它们移到Archive文件夹的工作。但是,我正在寻找要逐个拾取的文件,将日期保存到目标工作簿中,然后将其移动到存档文件夹中,此过程一直进行到最后一个工作簿为止。我使用下面的代码复制数据,并调用Sub例程执行存档。 但是在第一次迭代中,它会将所有文件一次移到“存档”文件夹中。

Sub Test()
    Dim xWb As Workbook
    Dim xToBook As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xFiles As New Collection
    Dim I As Long
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
    xFile = Dir(xStrPath & "*.xlsx")
    If xFile = "" Then
        MsgBox "No files found", vbInformation, "Kutools for Excel"
        Exit Sub
    End If
    Do While xFile <> ""
        xFiles.Add xFile, xFile
        xFile = Dir()
    Loop
    Set xToBook = ThisWorkbook
    If xFiles.Count > 0 Then
        For I = 1 To xFiles.Count
            Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
            xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
            On Error Resume Next
            ActiveSheet.Name = xWb.Name
            On Error GoTo 0
            xWb.Close False
            '**Call MoveFiles_3**
        Next I
    End If
End Sub

Sub MoveFiles_3()
    Dim fso As Object, d As String, ext, x
    Dim srcPath As String, destPath As String, srcFile As String
    srcPath = "C:\Users\userfolder\Desktop\Test Macro\"
    destPath = "C:\Users\userfolder\Desktop\Archive Test\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    ext = Array("*.xlsx")
    MsgBox Dir(srcPath)
    For Each x In ext
        d = Dir(srcPath & x)
        Do While d <> ""
            srcFile = srcPath & d
            fso.CopyFile srcFile, destPath & d
            Kill srcFile
        d = Dir
        Loop
    Next x
    MsgBox "done"
End Sub

1 个答案:

答案 0 :(得分:0)

尝试以下代码。我在您的imgBG.image = imgBG.image!.withRenderingMode(.alwaysTemplate) imgBG.tintColor = UIColor.red 中添加了两行,Test是不必要的。

MoveFiles_3