在 VBA 中调用 Sub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7715044/
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-08 12:01:55  来源:igfitidea点击:

Calling a Sub in VBA

vbasyntax-errorcall

提问by reggie

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?

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

回答by ipr101

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?

至于原因,这来自 MSDN 通过这个问题 - Call 关键字在 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.

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

回答by Sam

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 Callkeywords is only really in VBA for backwards compatibilty and isn't actually required.

Call关键字是唯一真正在VBA的向后兼容性和实际上是不需要的。

Ifhowever, you decide to use the Callkeyword, then you have to change your syntax to suit.

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

'// 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 Callkeyword and the parentheses

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