如何只复制/粘贴所有文件夹和所有子文件夹中的某些文件?

时间:2018-09-18 02:18:34

标签: excel-vba

我正在尝试提出一个例程,该例程仅将某些文件复制到目录以及所有子目录中,然后将每个复制的文件粘贴到目标目录中。我想到了下面的代码,该代码将所有文件(在过滤列表中)复制到目标文件夹中,但我不知道如何在层次结构中进行递归循环。任何对此的指导将不胜感激。

Sub CopyFilteredFiles()

Dim rng As Range, cell As Range
Dim sht As Worksheet
Dim LastRow As Long
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim lastChar As Integer
Dim fileName As String

DestinationFolder = "C:\Users\ryans\OneDrive\Desktop\AllYAML\"
Set sht = ActiveSheet
LastRow = sht.Cells(sht.Rows.Count, "D").End(xlUp).Row

Set rng = Range("D14:D" & LastRow)
Set FSO = CreateObject("scripting.filesystemobject")

    For Each cell In rng.SpecialCells(xlCellTypeVisible)
        If cell.Value <> "" Then
        CopyFile = cell.Value
        Debug.Print cell.Value
            lastChar = InStrRev(CopyFile, "\")
            fileName = Mid(CopyFile, lastChar + 1, 199)
            On Error Resume Next

            FSO.CopyFile Source:=CopyFile, Destination:=DestinationFolder & fileName

        End If
    Next cell

End Sub

0 个答案:

没有答案