qua*_*ant 5 excel vba excel-2016
我有一个函数,它在某处有一些错误,导致#VALUE当我尝试在 excel 中执行它时返回。
我不知道错误在哪里,单步执行代码很乏味。所以我希望调试器在发生错误时立即中断。
我试着去Tools->options->General->"Break on All Errors"但没有发现任何变化。
如何让 VBA IDE 因错误而中断?
只需在您的函数中添加一个错误处理程序,如下所示。如果发生错误,IDE 将在立即窗口中打印错误描述并停止在 上Debug.Assert 0。然后按两次 F8 转到发生错误的行。
Function Test() As Variant
On Error GoTo ErrHandler
Dim v(), n&, r&, c&
For r = 1 To 3
For c = 1 To 4
n = n + 1
ReDim Preserve v(1 To r, 1 To c)
v(r, c) = n
Next c
Next r
Test = v
Exit Function
ErrHandler:
Debug.Print Err.Number & ": " & Err.Description
Debug.Assert 0
Resume
End Function
Run Code Online (Sandbox Code Playgroud)
就像是:
Public Function dividddeee(a As Variant, b As Variant) As Double
On Error GoTo wtf
dividddeee = a / b
Exit Function
wtf:
On Error GoTo 0
MsgBox "Houston, we've had a problem here"
MsgBox a & vbCrLf & b
End Function
Run Code Online (Sandbox Code Playgroud)