首页 > 解决方案 > 如何使用 VBA 将文件从一个目录复制到另一个目录

问题描述

我希望能够将 excel 文件从一个文件夹复制到另一个文件夹。但是,每次复制它时,我都想将名称更改为 TABLEAUPCQ1、TABLEAUPCQ2、TABLEAUPCQ3 等等。

原因是,如果有人已经在其他路径中使用它,我无法覆盖 excel 文件。

Sub CopierFichier()

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")

Call fso.CopyFile("R:\ASP\PCQ_BEHESP\SuiviPCQAssignations\TABLEAUPCQ.xlsm", "V:\DGI\11000_Surveillance\11200_InstallationsSousPression\PCQ\Suivi\TABLEAUPCQ.xlsm")

End Sub


标签: excelvba

解决方案


在单元格值内的每个子执行后,计数器加 1。这样,您就可以将当前计数存储在函数之外。

抓住它,使用它,增加它。

Sub CopierFichier()

Dim counter_var As String
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")

counter_var = Cells(1, 1).Value

Call fso.CopyFile("R:\ASP\PCQ_BEHESP\SuiviPCQAssignations\TABLEAUPCQ.xlsm", "V:\DGI\11000_Surveillance\11200_InstallationsSousPression\PCQ\Suivi\TABLEAUPCQ_COPIE_" + counter_var + ".xlsm")

Cells(1, 1).Value = Cells(1, 1).Value + 1

End Sub

我纠正了这个小错误。


推荐阅读