【问题标题】:Why `On Error Resume Next` did not skip error `File not found` on the below code?为什么`On Error Resume Next`没有跳过以下代码中的错误`File not found`?
【发布时间】: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上设置,无论如何提供的答案有效。

标签: excel vba


【解决方案1】:

如果第一个失败,您只想再次执行Name。更传统的错误处理是这样的

Sub Name_Test()
    On Error GoTo EH
 
    Dim ErrCnt As Long
    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
Exit Sub

TryAgain:
    Application.Wait Now + TimeValue("00:00:01")
    Name oldName As newName
    ThisWorkbook.Save
Exit Sub

EH:
    ErrCnt = ErrCnt + 1
    If ErrCnt = 1 Then
        Resume TryAgain
    EndIf
End Sub

【讨论】:

  • 它工作得很好,但是请:你的答案advantages 比较“FaneDure” 的答案(除了添加Exit Sub)?
  • @user22 On Error GoTo -1 没有被微软记录,我已经看到有信誉的来源警告不要使用它(我认为是 cpearson.com,但我在 atm 找不到它)。正如我向您展示的那样,您不必使用它,那么为什么要冒险。此处显示的所有其他代码都有总是执行重试的错误。恕我直言,通过我的代码的逻辑流程更加清晰
【解决方案2】:

请试试这个更新的代码。它使用On Error GoTo -1从内存中清除之前的错误:

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:

    Application.Wait Now + TimeValue("00:00:01")

     On Error GoTo -1 'it clears the previous error from memory!
     
     On Error Resume Next
     Name oldName As newName
     If Err.Number <> 0 Then Stop 'it has been caught, but no error has been raised, anymore...
      'ThisWorkbook.Save
End Sub

"注意:Err.Clear 与 On Error Goto -1 不同。Err.Clear 仅清除错误描述和错误编号。在On Error GoTo label 的情况下,它不会完全重置它(从内存中) .这意味着如果在同一代码中有另一个错误实例,您将无法在重置之前处理它,可以使用On Error Goto -1而不使用Err.Clear来完成“

几年前,我在搜索以澄清这方面时发现了上述注释(或具有类似含义的内容)......

【讨论】:

  • @user22 很高兴我能帮上忙!我还在修改后的代码后放了一个注释。我记得我在测试环境中的某个地方找到了它......就像我在那里写的那样,我不是它的父亲。我在互联网上的某个地方找到了它(我不记得在哪里)。不完全是同一个词。一段时间后,我写了它(在我的工作簿中),据我所知,考虑到它很重要......
  • 这个错误的代码。如果第一个Name succeeds, the code will continue and execute the second Name`
  • 我回复为已接受(尽管它有效)@FaneDuru,因为更多 reliable 答案。
  • @user22 我对此没有意见。我只回答了这个问题:为什么On Error Resume Next 没有跳过错误。我永远不会使用这样的代码。我只是尝试解释应该如何处理这样的错误。如果您能解释一下您尝试完成的工作,我想我可以建议另一种处理方式。如果您需要等待保存工作簿的过程或类似的东西,我也可以就这个问题提出建议。但是,设置接受的答案只是您的选择!无论如何,我不太在乎恶名。这只是遵守规则的问题
【解决方案3】:

On Error GoTo -1 可以有效地关闭错误处理程序,但不会出现在 Microsoft 文档中 On Error statement

如果你在未来的开发中发现它是一个问题,你应该能够避免它。

Sub Name_Test()

    On Error GoTo Pause
    Debug.Print "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
    
    ' To test, comment line above
    Debug.Print " No error."
    
Pause:
    
    If Err <> 0 Then
        Debug.Print Err.Number, Err.Description
        Resume resetErrorhandlerPause
    Else
        On Error GoTo 0
    End If
    
resetErrorhandlerPause:

    On Error Resume Next
    Debug.Print "On Error Resume Next"
    
    Application.Wait Now + TimeValue("00:00:01")
    Name oldName As newName
    If Err <> 0 Then
        Debug.Print Err.Number, Err.Description
        Debug.Print " Error bypassed with On Error Resume Next"
    End If
    
    ' ThisWorkbook.Save
    
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 2017-10-01
    • 2011-06-17
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多