根据excel中的列表将文件从子文件夹复制到一个文件夹

时间:2018-11-13 15:25:51

标签: excel vba excel-vba

我有两个文件,它们位于两个不同的子文件夹中。请参考以下屏幕截图:

Screen Grabo of filename and Path

当前,我正在使用以下宏根据excel中的文件列表将文件从一个文件夹复制到另一个文件夹(所有这些文件都在一个文件夹中),但是它不允许从子文件夹复制文件:

Sub copyfiles()
    Dim xRg As Range, xCell As Range
    Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
    Dim xSPathStr As Variant, xDPathStr As Variant
    Dim xVal As String

    On Error Resume Next

    Set xRg = Application.InputBox("Please select the file names:", "KuTools For Excel", ActiveWindow.RangeSelection.Address, , , , , 8)

    If xRg Is Nothing Then Exit Sub

    Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xSFileDlg.Title = "Please select the original folder:"

    If xSFileDlg.Show <> -1 Then Exit Sub

    xSPathStr = xSFileDlg.SelectedItems.Item(1) & "\"
    Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xDFileDlg.Title = "Please select the destination folder:"

    If xDFileDlg.Show <> -1 Then Exit Sub

    xDPathStr = xDFileDlg.SelectedItems.Item(1) & "\"

    For Each xCell In xRg
        xVal = xCell.Value

        If TypeName(xVal) = "String" And xVal <> "" Then
            FileCopy xSPathStr & xVal, xDPathStr & xVal
        End If
    Next
End Sub

如果我对2个子文件夹中的文件使用相同的宏,则会出现“找不到文件”错误。 那么,有没有办法修改同一个宏,以允许我从子文件夹复制文件?

0 个答案:

没有答案