我正在编写VBA代码以将文件从一个共享点路径移动到另一个共享点路径。文件移动后,我想在下一列中将“ File Moved”显示为实际文件名。请在下面找到我的代码。
currRow = 11
On Error Resume Next
For currRow = currRow To LastRow
Filename = ActiveSheet.Range("A" & currRow).Value
From_Path = "blabla"
To_Path = "blabla"
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.MoveFile From_Path & Filename, To_Path & Filename
ActiveSheet.Range("B" & currRow) = "File Moved"
ThisWorkbook.Save
Next currRow
MsgBox "Files moved"
但是问题是我由于某些错误而无法移动的文件正在“文件移动”。
感谢您的帮助。
答案 0 :(得分:2)
您可以执行以下操作。最好限制On Error Resume Next
,以免在其余代码中忽略“意外”错误:
Dim msg
From_Path = "blabla"
To_Path = "blabla"
Set FSO = CreateObject("Scripting.FileSystemObject")
For currRow = currRow To LastRow
Filename = ActiveSheet.Range("A" & currRow).Value
On Error Resume Next
FSO.MoveFile From_Path & Filename, To_Path & Filename
If Err.Number<>0 Then
msg = "Error: " & Err.Description
Else
msg = "File moved"
End If
On Error Goto 0 'limit scope for ignoring errors...
ActiveSheet.Range("B" & currRow) = msg
ThisWorkbook.Save
Next currRow