我知道有一些关于此主题的类似文章。但是,我有一个与我在这里看到的所有代码都不同的代码(当谈论这个主题时)。使用vba将文件从一个文件夹复制到另一个文件夹
我收到的错误是说该文件找不到。但这是不可能的,因为我正在使用fso.CopyFile中的SOURCE文件搜索文件。
所以我必须解决这个错误,如果可能的话,我想将文件复制到另一个文件夹并更改名称。例如,如果我有文件“Excel.xls”,我想复制名称“Excel_old.xls”,使用下面的代码可能会有这种可能吗?或者它太难了,它不值得?
这是代码:
Sub CopyFiles()
'Macro to copy all files modified yesterday
Dim n As String, msg As String, d As Date
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set fils = fso.GetFolder("C:\Users\Desktop\Files\").Files
'Verify all files in the folder, check the modification date and then copy
'to another folder (named Old)
For Each fil In fils
n = fil.Name
d = fil.DateLastModified
If d >= Date - 1 Then
file = n
'The following line is where the error occurs
fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"
End If
Next fil
End Sub
这是正确的。但是在'fso.CopyFile'之后有一个错字,根据OP的代码它应该是'file'或'fil.name'。 – ZAT 2014-10-07 18:11:06
大佬们!修复。 – JNevill 2014-10-07 18:14:26
但我仍然有错误13类型不匹配 – dekio 2014-10-07 18:28:14