VBA 函数调用中括号的使用规则是什么? [英] What are the rules governing usage of parenthesis in VBA function calls?

查看:34
本文介绍了VBA 函数调用中括号的使用规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚经历了令人恼火的 30 分钟编译器错误";在 VBA (Access 2003) 中,由于我在传递给我定义的 Sub 的参数周围使用了括号.

I've just had an irritating 30 minutes on a "compiler error" in VBA (Access 2003) caused by my use of parenthesis around the arguments I'm passing to a Sub I defined.

我一直在寻找有关何时需要/适当/不适当/禁止使用括号的体面文章/教程/说明,但找不到任何明确的指导方针.

I've been searching to find a decent article/tutorial/instruction as to when parenthesis are necessary/appropriate/inappropriate/forbidden, but can't find any clear guidelines.

推荐答案

来自 这里:

使用 VBScript 调用语句调用子程序当您希望调用子程序时,Call 语句的使用是可选的.与 Sub 一起使用时,Call 语句的目的是允许您将参数列表括在括号中.但是,如果子例程不传递任何参数,则在使用 Call 语句调用 Sub 时仍然不应使用括号.

Using the VBScript Call Statement to Call a Subroutine The use of Call statement is optional when you wish to call a subroutine. The purpose of the Call statement when used with a Sub is to allow you to enclose the argument list in parentheses. However, if a subroutine does not pass any arguments, then you still should not use parentheses when calling a Sub using the Call statement.

Call MySubroutine

如果子例程有参数,则在使用 Call 语句时必须使用括号.如果有多个参数,您必须用逗号分隔参数.

If a subroutine has arguments, you must use parentheses when using the Call statement. If there is more than one argument, you must separate the arguments with commas.

Call MySubroutine(intUsageFee, intTimeInHours, "DevGuru") 

调用函数有两种可能的方法来调用函数.您可以直接调用该函数,仅通过名称,或者您可以使用 VBScript Call 语句调用它.

Calling the Function There are two possible ways to call a function. You may either call the function directly, by name only, or you may call it by using the VBScript Call statement.

按名称调用函数当直接按名称调用函数并且没有对返回值赋值时,以下所有语法都是合法的:

Calling a Function by Name When calling a function directly by name and when there is no assignment to a returned value, all of the following are legal syntax:

MyFunction
MyFunction()
MyFunction intUsageFee, intTimeInHours, "DevGuru"

如果你想要一个返回值,你可以将函数赋值给一个变量.请注意,如果有一个或多个参数,则必须使用括号.

If you want a returned value, you can assign the function to a variable. Note that if there is one or more arguments, you must use the parentheses.

returnval = MyFunction
returnval = MyFunction()
returnval = MyFunction(intUsageFee, intTimeInHours, "DevGuru") 

这篇关于VBA 函数调用中括号的使用规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆