两者的区别
Range("A1").Copy (Range("E5"))
和
Range("A1").Copy Range("E5")
是第一个将ByRef 参数转换为ByVal 而第二个没有。此处的括号代表转换,而不是您所期望的。
请注意,您只能在使用函数(返回值)时使用括号,而在使用过程时不能。
例子
a = MyFunction(Parameter1, Parameter2) 'correct with parenthesis
MyProcedure Parameter1, Parameter2 'correct w/o parenthesis
a = MyFunction Parameter1, Parameter2 'it will error
MyProcedure(Parameter1, Parameter2) 'it will error
现在只有一个参数
a = MyFunction(Parameter1) 'correct with parenthesis
MyProcedure Parameter1 'correct w/o parenthesis
a = MyFunction Parameter1 'it will error
MyProcedure (Parameter1) 'it will NOT error but convert ByRef into ByVal !!!
上面例子的最后一行做了一些与你预期完全不同的事情。您可以注意到过程名称和括号之间的额外空格。和下面的语法一样
a = MyFunction((Parameter1), (Parameter2)) 'convert ByRef into ByVal
MyProcedure (Parameter1), (Parameter2) 'convert ByRef into ByVal
所以我们可以记下来:
- 如果函数返回一个我们想要写入变量的值:
您必须使用括号:a = MyFunction(Parameter1)
- 如果过程(或函数)不返回值,则:
您不得使用括号:MyProcedure Parameter1