跳至主要内容

如何根据Excel中的列表将文件从一个文件夹复制或移动到另一个文件夹? 

Author: Xiaoyang Last Modified: 2025-07-31

如果在工作表的列中有一组文件名,而这些文件位于您计算机的某个文件夹中。但是现在,您需要将这些文件(其名称已列在工作表中)从原始文件夹移动或复制到另一个文件夹,如下图所示。如何在Excel中尽快完成此任务呢?

使用VBA代码根据Excel中的列表将文件从一个文件夹复制或移动到另一个文件夹

copy files from one folder to another based on a list


使用VBA代码根据Excel中的列表将文件从一个文件夹复制或移动到另一个文件夹

要根据文件名列表将文件从一个文件夹移动到另一个文件夹,以下VBA代码可能会对您有所帮助,请按照以下步骤操作:

1. 在Excel中按住 Alt + F11 键,打开 Microsoft Visual Basic for Applications 窗口。

2. 单击 插入 > 模块,并将以下VBA代码粘贴到模块窗口中。

VBA代码:根据Excel中的列表将文件从一个文件夹移动到另一个文件夹

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

3. 然后按 F5 键运行此代码,会弹出一个提示框,提醒您选择包含文件名的单元格,见截图:

 select the cells which contain the file names

4. 然后单击“ 确定”按钮,在弹出的窗口中,请选择包含要移动文件的文件夹,见截图:

select the folder which contains the files to move from

5. 然后单击“确定”,继续在另一个弹出的窗口中选择要存放文件的目标文件夹,见截图:

select the destination folder  to locate the files

6. 最后,单击“确定”关闭窗口,现在,文件已经根据工作表列表中的文件名被移动到您指定的另一个文件夹中,见截图:

the files have been moved into another folder

备注:如果您只想将文件复制到另一个文件夹,但保留原始文件,请使用以下VBA代码:

VBA代码:根据Excel中的列表将文件从一个文件夹复制到另一个文件夹

Sub copyfiles()
'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
        End If
    Next
End Sub
a screenshot of kutools for excel ai

使用 Kutools AI 解锁 Excel 魔法

  • 智能执行:执行单元格操作、分析数据和创建图表——所有这些都由简单命令驱动。
  • 自定义公式:生成量身定制的公式,优化您的工作流程。
  • VBA 编码:轻松编写和实现 VBA 代码。
  • 公式解释:轻松理解复杂公式。
  • 文本翻译:打破电子表格中的语言障碍。
通过人工智能驱动的工具增强您的 Excel 能力。立即下载,体验前所未有的高效!

最佳 Office 办公效率工具

🤖 Kutools AI 助手:基于智能执行,彻底革新数据分析 生成代码创建自定义公式分析数据并生成图表调用 Kutools Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!