gpt4 book ai didi

vba - 如何将引发的错误传递给 VBA 中的自定义错误处理程序?

转载 作者:行者123 更新时间:2023-12-01 22:49:01 25 4
gpt4 key购买 nike

我已经在 Excel 中使用 VBA 一段时间了,并且我对所有过程都使用了自定义错误处理程序。我发现自己第一次需要使用 Err.Raise (来处理 Select Case 中的 Case Else 情况) code> block)。,我不知道如何将错误传递给自定义错误处理程序。 VBA 没有将引发的错误传递给自定义处理程序,而是弹出自己的丑陋且相当无用的错误对话框。如果有人能告诉我解决这个问题的方法,我将非常感激。

下面是我正在使用的代码的通用版本(函数/变量名称已更改以保护无辜者)。 gErrorHandler 对象是一个全局维度的类模块变量,它处理来自任何和所有过程的错误。

Public Function MyFunction(dblInputParameter As Double) As Double
On Error GoTo Err_MyFunction

Dim dblResult as Double

Select Case dblInputParameter

...Several case statements go here...

Case Else
Err.Raise vbObjectError + 1000, "MyProjectName.MyObjectName", "Error Description"

End Select

MyFunction = dblResult

Exit_MyFunction:
Exit Function

Err_MyFunction:
gErrorHandler.DisplayError Err.Number, Err.Description, Erl, csModule, "basMyModuleName", "MyFunction"
Resume Exit_MyFunction

End Function

这是我得到的错误对话框,而不是将错误传递给自定义处理程序:

enter image description here

最佳答案

正如 Tim 在评论中指出的那样,答案是 VBA IDE 被配置为在出现所有错误时中断。将其更改为在未处理的错误上中断只会给我带来我想要的行为。

enter image description here

关于vba - 如何将引发的错误传递给 VBA 中的自定义错误处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com