在 VBA 中调用子程序

发布于 2024-12-08 19:43:25 字数 410 浏览 0 评论 0原文

这是我的简化脚本:

    Sub SomeOtherSub(Stattyp As String)
        'Daty and the other variables are defined here

        CatSubProduktAreakum(Stattyp, Daty + UBound(SubCategories) + 2)

    End Sub

    Sub CatSubProduktAreakum(Stattyp As String, starty As Integer)

    'some stuff

    End Sub

CatSubProduktAreakum 的调用被标记为红色作为“语法错误”。我不明白这个错误。这是一个带有两个参数的简单子例程调用。为什么VBA不接受调用?

This my simplified script:

    Sub SomeOtherSub(Stattyp As String)
        'Daty and the other variables are defined here

        CatSubProduktAreakum(Stattyp, Daty + UBound(SubCategories) + 2)

    End Sub

    Sub CatSubProduktAreakum(Stattyp As String, starty As Integer)

    'some stuff

    End Sub

The call of CatSubProduktAreakum is marked red as a "syntax error". I don't understand the error. It is a simple sub-routine call with two arguments. Why does VBA not accept the call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

燃情 2024-12-15 19:43:25

尝试 -

Call CatSubProduktAreakum(Stattyp, Daty + UBound(SubCategories) + 2)

至于原因,这是来自 MSDN 通过这个问题 - 什么Call关键字在VB6中做什么?

调用过程时不需要使用 Call 关键字。
但是,如果您使用 Call 关键字来调用需要
参数、参数列表必须括在括号内。如果你省略
Call 关键字,您还必须省略两边的括号
参数列表。如果您使用 Call 语法来调用任何内在函数或
用户定义的函数,函数的返回值被丢弃。

Try -

Call CatSubProduktAreakum(Stattyp, Daty + UBound(SubCategories) + 2)

As for the reason, this from MSDN via this question - What does the Call keyword do in VB6?

You are not required to use the Call keyword when calling a procedure.
However, if you use the Call keyword to call a procedure that requires
arguments, argumentlist must be enclosed in parentheses. If you omit
the Call keyword, you also must omit the parentheses around
argumentlist. If you use either Call syntax to call any intrinsic or
user-defined function, the function's return value is discarded.

如何视而不见 2024-12-15 19:43:25

对于仍然阅读本文的人,另一个选择是简单地省略括号:

Sub SomeOtherSub(Stattyp As String)
    'Daty and the other variables are defined here

    CatSubProduktAreakum Stattyp, Daty + UBound(SubCategories) + 2

End Sub

Call 关键字在 VBA 中实际上只是为了向后兼容,实际上并不是必需的。

但是如果您决定使用Call 关键字,那么您必须更改语法以适应。

'// With Call
Call Foo(Bar)

'// Without Call
Foo Bar

两者都会做完全相同的事情。


话虽这么说,但在某些情况下,可能需要注意不必要地使用括号会导致在您不希望的情况下对事物进行评估(因为括号在 VBA 中就是这样做的),因此考虑到这一点,更好的选择可能是省略 Call 关键字和括号

For anyone still coming to this post, the other option is to simply omit the parentheses:

Sub SomeOtherSub(Stattyp As String)
    'Daty and the other variables are defined here

    CatSubProduktAreakum Stattyp, Daty + UBound(SubCategories) + 2

End Sub

The Call keywords is only really in VBA for backwards compatibilty and isn't actually required.

If however, you decide to use the Call keyword, then you have to change your syntax to suit.

'// With Call
Call Foo(Bar)

'// Without Call
Foo Bar

Both will do exactly the same thing.


That being said, there may be instances to watch out for where using parentheses unnecessarily will cause things to be evaluated where you didn't intend them to be (as parentheses do this in VBA) so with that in mind the better option is probably to omit the Call keyword and the parentheses

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