【问题标题】:Check for the current error handling in VBA检查 VBA 中的当前错误处理
【发布时间】:2022-06-13 13:11:24
【问题描述】:

在 VBA 中,错误处理由 on error 语句完成。

我想暂时更改错误处理,然后再回到以前的行为。如何检查当前的错误处理并将其存储在变量中(我在引用中找不到任何内容)?

'set the error handling to s.th. "on error... "
'some code with the regular error handling

'change the error handling to "on error ..." (regardless of what it was before)
'some code with the new error handling

'change back to the previous error handling
'some code with the regular error handling

背景:我需要对 Variant 数组进行is nothing 检查以排除使用空对象索引,但将is nothing 应用于包含值的数组索引会引发异常,所以我暂时想更改对on error resume next 的错误处理。最终使用不同的方法解决了这个问题,但我仍然想知道我是否可以在运行时以某种方式确定当前的错误处理Here's the question and answer to my original problem

编辑:我知道我可以手动检查我以前的代码以找出使用了哪种类型的错误处理。但是我想避免这种情况(以节省时间)。

我想作为一种解决方法,我可以设置一个带有状态的附加变量,然后我可以检查当前状态,尽管这会导致相当多的开销。像这样的:

Dim errorHandling as String

errorHandling = "resumeNext"
on error resume next
'some code

'changing the error handling temp.
'some other code

'changing the error handling to it's previous state
if errorhandling = "resumeNext" then
   On Error Resume Next
elseif errorhandling = "GoToErrorhandler" then
   On Error GoTo errorhandler
End If

'Rest of the code

【问题讨论】:

  • 您需要将代码添加到此问题中。另外,至少,添加您填充数组的方式。当你声明一个数组As Variant 时,每个元素都被赋值为Empty,而不是Nothing,因此你需要做类似If IsEmpty(MyArray(i)) ThenIf Not IsEmpty(MyArray(i)) Then 的操作。你为什么不分享完整的代码,这样你就可以看到它是如何应用的。经验法则是使用错误捕获(处理)作为“最后的手段”,即当没有其他(简单)方法时。
  • @VBasic2008 这个问题与数组无关,我只是想暂时更改错误处理(无需手动检查之前的内容)。
  • 你为什么不解释一下temporarily change the error handling是什么意思?没有解释,对我来说没有任何意义。也许在您的帖子中添加一些代码或伪代码。
  • 抱歉我的编辑速度不够快...

标签: vba error-handling


【解决方案1】:

读取/写入数组

Option Explicit

Sub ReadWriteArrayExample()

    Dim myArray() As Variant: ReDim myArray(1 To 10)
    
    Dim i As Long
    Dim n As Long
    
    ' Fill the array.
    For i = 1 To 10
        n = Application.RandBetween(0, 1)
        If n = 1 Then ' write a random number between 1 and 10 inclusive
            myArray(i) = Application.RandBetween(1, 10)
        'Else ' "n = 0"; leave the element as-is i.e. 'Empty';do nothing
        End If
    Next i
    
    ' Debug.Print the result.
    Debug.Print "Position", "Value"
    For i = 1 To 10
        If Not IsEmpty(myArray(i)) Then ' write the index and the value
            Debug.Print i, myArray(i)
        'Else ' is empty; do nothing
        End If
    Next i

End Sub

错误处理

Sub ErrorHandling()

    Const ProcName As String = "ErrorHandling"
    On Error GoTo ClearError ' enable error trapping

    ' Some code

    On Error Resume Next ' defer error trapping
        ' Some tricky code
    On Error GoTo ClearError ' re-enable error trapping

    ' Some Code

ProcExit:
    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
    
End Sub

【讨论】:

  • 如果我没记错的话只检查整个数组是否为空?!
  • 您可以安全地运行(它只写入即时窗口)第一个代码,看看它做了什么。运行几次看看有什么不同。
  • 感谢您的第二个建议(错误代码处理),不幸的是,它没有给我想要的结果。 On Error GoTo ClearError ' re-enable error trapping 仅在先前的错误处理设置为 On Error GoTo ClearError 时才有效。我想避免这种情况,并有一种更动态的方法,我不必手动跟踪错误处理。我的问题可能不够清楚,对此感到抱歉。
  • 关于数组的建议,它仍然给我一个错误:isempty() 不会捕获空对象 (Set myArray(1) = Nothing) 导致 debug.print 行出现异常。但是我已经使用 isobject() 解决了这个问题(请参阅我的另一个问题)。不过还是谢谢你的建议。
猜你喜欢
  • 2023-03-18
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多