如何从 VBA 函数触发错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3051051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:44:16  来源:igfitidea点击:

How to Trigger a Error from a VBA function

excelvba

提问by nimo

I need to trigger(return) an error event from a VBA function, then the calling function of this function can trigger On Error Go to call. E.g

我需要从 VBA 函数触发(返回)一个错误事件,然后这个函数的调用函数可以触发 On Error Go 调用。例如

function Test()
 On Error Go to myError:
       TestErr()
 Exit Function

 myerror:
    Test = "Error Triggered"
End Function

Function TestErr()
    ?? 'How to Trigger error here
End Function

Thank You

谢谢你

回答by Russel Yang

Err.Raise 5, "optional error source" , "optional error description"

Err.Raise 5、“可选错误源”、“可选错误描述”

MSDN reference http://msdn.microsoft.com/en-us/library/aa164019%28office.10%29.aspx#odc_tentipsvba_topic3

MSDN 参考http://msdn.microsoft.com/en-us/library/aa164019%28office.10%29.aspx#odc_tentipsvba_topic3

回答by nimo

Dirty way: 1 / 0

肮脏的方式: 1 / 0

回答by Dick Kusleika

Not what you asked, but note that if you want to return an error to a cell from a UDF, use CVErr. Like

不是您问的问题,但请注意,如果您想从 UDF 向单元格返回错误,请使用 CVErr。喜欢

Test = CVErr(xlErrNA)

to return #NA

返回#NA