ByRef 似乎接收值而不是 VBA 6.0 中的引用 [英] ByRef seems to receive the value and not the reference in VBA 6.0
问题描述
我的小示例代码
Function AddNr(ByRef x As Integer) As Integer
x = x + 2
AddNr = x
End Function
Sub test()
Dim ana As Integer
ana = 1
AddNr (ana)
MsgBox ana
End Sub
应该输出3但输出1.更具体地说,ana
变量在调用AddNr
函数后不会被修改.
should output 3 but outputs 1. To be more specific the ana
variable is not modified after the call to the AddNr
function.
我的环境是 Excel 2007 中的 Microsoft Visual Basic 6.5.
My environment is Microsoft Visual Basic 6.5 inside Excel 2007.
推荐答案
那应该是:
AddNr ana
也就是说,没有括号.
来自微软帮助:
备注
您不需要使用呼叫调用过程时的关键字.但是,如果您使用 Call 关键字调用一个需要的过程参数,参数列表必须是括在括号中.如果省略Call 关键字,您也必须省略参数列表周围的括号.如果您使用 Call 语法来调用任何内在的或用户定义的函数,函数的返回值被丢弃.
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.
这篇关于ByRef 似乎接收值而不是 VBA 6.0 中的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!