首页 > 解决方案 > 如果 Excel 文件中存在该文件的名称,如何将文件从一个位置复制到另一个位置

问题描述

我有一个包含图像文件名称的 Excel 文件。在同一个文件夹中,我有 Excel 文件中列出的所有图像以及一些附加图像。现在我要做的是分离 Excel 文件中列出的所有图像。Excel 文件中列出的图像应复制到另一个文件夹。我已按照本文作者的指示使用了该方法:

https://www.extendoffice.com/documents/excel/4775-move-files-based-on-excel-list.html

Sub movefiles()
'Updateby Extendoffice
    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
            Kill xSPathStr & xVal
        End If
    Next
End Sub

但是当我执行代码时没有任何反应。有什么方法可以完成任务吗?

标签: excelvba

解决方案


推荐阅读