现在的位置: 首页 > 综合 > 正文

VBA语法中括号的使用

2013年10月20日 ⁄ 综合 ⁄ 共 1101字 ⁄ 字号 评论关闭

不同语言函数调用的语法有不同的,比如C++函数调用要加括号的,但是在VB中加不加括号要视情况而定,该不加的地方加了也是语法错误。

以下是来自VBA帮助中的原话:

The syntax for functions and some methods shows the arguments enclosed in parentheses. These functions and methods return values, so you must enclose the arguments in parentheses to assign the value to a variable. If you ignore the return value or if you don't pass arguments at all, don't include the parentheses. Methods that don't return values do not need their arguments enclosed in parentheses. These guidelines apply whether you're using positional arguments or named arguments.

另外一篇帮助文件:

Sub procedures, built-in statements, and some methods don't return a value, so the arguments aren't enclosed in parentheses. For example:

MySub "stringArgument", integerArgument

Function procedures, built-in functions, and some methods do return a value, but you can ignore it. If you ignore the return value, don’t include parentheses. Call the function just as you would call a Sub procedure. Omit the parentheses, list any arguments , and don't assign the function to a variable. For example:

MsgBox "Task Completed!", 0, "Task Box"

To use the return value of a function, enclose the arguments in parentheses。

总结一下:用到返回值,加括号;没用到返回值,不要加括号。函数或方法的语法里有括号,表示它有返回值,但是在语句里如果不利用这个返回值赋给变量的话,调用函数或方法不要加括号。

抱歉!评论已关闭.