【发布时间】:2022-02-15 07:36:20
【问题描述】:
使用On Error Resume Next 跳过以下代码中的任何错误,但它不起作用。
我收到错误找不到文件 on the second VBA name command。
我知道可以修改代码以在重命名之前检查文件是否存在。
我需要知道为什么 On Error Resume Next 没有跳过那个错误。
感谢您的帮助。
Sub Name_Test()
On Error GoTo Pause
Dim oldName As String: oldName = "C:\Users\Waleed\Desktop\Test.txt"
Dim newName As String: newName = "C:\Users\Waleed\Desktop\Done.txt"
Name oldName As newName
Pause:
On Error Resume Next
Application.Wait Now + TimeValue("00:00:01")
Name oldName As newName
ThisWorkbook.Save
End Sub
【问题讨论】:
-
尝试在
Pause:之后添加err.clear并且不要错过退出您的子系统以防出现错误。 -
@Ike 我尝试添加 err.clear ,但同样的错误
File not found -
之前的错误必须从内存中清除,但在这种情况下
err.clear不起作用。你必须使用On Error GoTo -1... -
检查VBE设置,可以设置为Break on all errors。
-
@Kostas K 我检查了 VB 设置,但它没有在
Break on all errors上设置,无论如何提供的答案有效。