Excel宏 - 移动文件(修复代码)

时间:2013-10-16 07:33:08

标签: excel file excel-vba move vba

我有这段代码:

Sub MoveFiles()
Dim d As String, ext, x
Dim srcPath As String, destPath As String, srcFile As String
srcPath = "C:\test\"
destPath = "C:\test2\"
ext = Array("*.csv", "*.xls")
For Each x In ext
d = Dir(srcPath & x)
    Do While d <> ""
        srcFile = srcPath & d
        FileCopy srcFile, destPath & d
        Kill srcFile
        d = Dir
    Loop
Next
End Sub

但它会删除srcPath中的每个文件。我只需要它来删除activeworkbook.name而不是每一个。 香港专业教育学院考虑了这个代码一个小时,并且无法弄清楚如何使它不循环并仍然做它应该做的。

我很感激这方面的帮助

1 个答案:

答案 0 :(得分:0)

如果你只想杀死一个特定的文件,为什么要使用一个循环来杀死它迭代中的每个文件?

如果要杀死特定文件并仍然循环,则需要添加“if”子句 类似的东西:

if srcFile = activewindow.name then
    kill
else
    do nothing
end if

或者我错了你?