VBA 出错时如何从函数返回
我是 VBA 新手,当我看到错误时希望从函数返回。无法这样做。有什么指点吗?
Function GetEditboxValue(control As IRibbonControl, text As String) As String
If Not IsMissing(text) Then
If Not IsNumeric(text) Then
MsgBox "Please enter numeric value only."
' I WANT TO RETURN HERE
End If
End If
If control.id = "xyz" Then
spaceAboveTable = text
End If
End Function
I am new to VBA and want to return from a function when I see an error. Not able to do so. Any pointers?
Function GetEditboxValue(control As IRibbonControl, text As String) As String
If Not IsMissing(text) Then
If Not IsNumeric(text) Then
MsgBox "Please enter numeric value only."
' I WANT TO RETURN HERE
End If
End If
If control.id = "xyz" Then
spaceAboveTable = text
End If
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将
EXIT FUNCTION
放在那里以退出进一步的执行:You need to put
EXIT FUNCTION
there to get out of further execution:从广义上讲(我实际上并没有说任何尚未说过的话):
当然,没有理由它必须是 Double 。
(编辑是因为我有第二个“退出功能”而不是“结束功能”。)
In a generalized sense (I'm not actually saying anything that hasn't already been said):
No reason it has to be Double, of course.
(Edited because I had a second "Exit Function" instead of "End Function.")
您可以在
MsgBox
语句后使用Exit Function
You can use
Exit Function
after yourMsgBox
statement另一种选择是使用
Err.Raise
显式引发错误。由于此函数内没有错误处理程序(即没有On Error ...
行),错误将通过调用堆栈向上传播,直到遇到错误处理程序。Another option is to explicitly raise an error using
Err.Raise
. Since you have no error handler within this function (ie, noOn Error ...
line) the error will be propagated up through the call stack until it runs into an error handler.