VBA 出错时如何从函数返回

发布于 2024-10-09 17:24:59 字数 421 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呆° 2024-10-16 17:25:00

您需要将 EXIT FUNCTION 放在那里以退出进一步的执行:

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."
            EXIT FUNCTION
        End If
    End If


    If control.id = "xyz" Then
    spaceAboveTable = text
    End If


End Function

You need to put EXIT FUNCTION there to get out of further execution:

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."
            EXIT FUNCTION
        End If
    End If


    If control.id = "xyz" Then
    spaceAboveTable = text
    End If


End Function
苏大泽ㄣ 2024-10-16 17:25:00

从广义上讲(我实际上并没有说任何尚未说过的话):

Function Foo(inputVar As Double) As Double

    On Error GoTo ErrorHandler

    ' Code assigning something to returnValue and defaultOutput

    Foo = returnValue

    Exit Function

    ErrorHandler:

    Foo = defaultOutput

 End Function

当然,没有理由它必须是 Double 。

(编辑是因为我有第二个“退出功能”而不是“结束功能”。)

In a generalized sense (I'm not actually saying anything that hasn't already been said):

Function Foo(inputVar As Double) As Double

    On Error GoTo ErrorHandler

    ' Code assigning something to returnValue and defaultOutput

    Foo = returnValue

    Exit Function

    ErrorHandler:

    Foo = defaultOutput

 End Function

No reason it has to be Double, of course.

(Edited because I had a second "Exit Function" instead of "End Function.")

甜尕妞 2024-10-16 17:25:00

您可以在 MsgBox 语句后使用 Exit Function

You can use Exit Function after your MsgBox statement

你列表最软的妹 2024-10-16 17:25:00

另一种选择是使用 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, no On Error ... line) the error will be propagated up through the call stack until it runs into an error handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文