ByRef 似乎接收值而不是 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。更具体地说,在调用 AddNr
函数后,ana
变量不会被修改。
我的环境是 Excel 2007 内的 Microsoft Visual Basic 6.5。
My little sample code
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
should output 3 but outputs 1. To be more specific the ana
variable is not modified after the call to the AddNr
function.
My environment is Microsoft Visual Basic 6.5 inside Excel 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Remou 已经把它搞定了,但我认为函数调用中括号的作用可以再充实一点。在过程调用中向参数添加额外一组括号会强制该参数按值传递,无论被调用的过程是希望按引用还是按值传递参数。 Microsoft 关于此主题的官方帮助页面位于:如何:强制按值传递参数 (Visual Basic)。
这个概念最容易通过一个例子来解释:
Remou nailed it already, but I thought the role of parentheses in function calls could be fleshed out a bit. Adding an extra set of parentheses to an argument in a procedure call forces that argument to be passed by value, regardless of whether the called procedure wants the argument by reference or by value. The official help page from Microsoft on this topic is here: How to: Force an Argument to Be Passed by Value (Visual Basic).
The concept is most easily explained by an example:
应该是:
即没有括号。
来自微软帮助:
That should be:
That is, no brackets.
From Microsoft Help: