我有工作表(「sheet2」)中的文件名列表。我必須檢查文件夾中的這些文件,如果文件存在,則應將其複製到新文件夾中。我不知道從哪裏開始。任何人都可以引導我嗎?如何通過查找位置將文件複製到另一個文件夾
Dim rngFile as Range, cel as Range
Dim desPath as String, filename as String, sourcePath as String
Set rngFile = Thisworkbook.Sheets("Sheet2").Range("A1","A500") ' file list in ColA
desPath = "D:\withdate\" 'Destination folder is withdate
sourcePath = "D:\All\All\(fetch each cell for file name?)" 'source folde
For Each cel in rngFile
If Dir(sourcePath & cel) <> "" Then
FileCopy sourcePath & cel, desPath & cel 'copy to folder
End If
Next
末次
但上面的代碼不復制文件!
什麼是rngfile?如何獲取sheet2中的文件名並檢查它的存在? – Vidhi
edited :)這就是你的清單所在。還添加了評論來指導您使用代碼。首先測試幾個樣品。 – L42
爲了回答第二個問題,'Dir'檢查文件是否存在。如果文件存在,它將返回文件名。 – L42