ByRef 似乎接收值而不是 VBA 6.0 中的引用

发布于 2024-10-15 08:44:40 字数 349 浏览 4 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-10-22 08:44:40

Remou 已经把它搞定了,但我认为函数调用中括号的作用可以再充实一点。在过程调用中向参数添加额外一组括号会强制该参数按值传递,无论被调用的过程是希望按引用还是按值传递参数。 Microsoft 关于此主题的官方帮助页面位于:如何:强制按值传递参数 (Visual Basic)

这个概念最容易通过一个例子来解释:

Sub Foo(ByRef Bar)
    Bar = 1
End Sub

Sub TestFoo()
Dim Bar
    Bar = 0
    Foo Bar   'The variable Bar is passed ByRef to Foo
    Debug.Print Bar '--> 1

    Bar = 0
    Foo (Bar)  'The expression (Bar) is evaluated and 
               '  the resultant value 0 is passed ByVal to Foo
    Debug.Print Bar '--> 0

    Bar = 0
    Call Foo(Bar)  'The variable Bar is passed ByRef to Foo
    Debug.Print Bar '--> 1

    Bar = 0
    Call Foo((Bar))  'The expression (Bar) is evaluated and 
                     '  the resultant value 0 is passed ByVal to Foo
    Debug.Print Bar '--> 0
End Sub

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:

Sub Foo(ByRef Bar)
    Bar = 1
End Sub

Sub TestFoo()
Dim Bar
    Bar = 0
    Foo Bar   'The variable Bar is passed ByRef to Foo
    Debug.Print Bar '--> 1

    Bar = 0
    Foo (Bar)  'The expression (Bar) is evaluated and 
               '  the resultant value 0 is passed ByVal to Foo
    Debug.Print Bar '--> 0

    Bar = 0
    Call Foo(Bar)  'The variable Bar is passed ByRef to Foo
    Debug.Print Bar '--> 1

    Bar = 0
    Call Foo((Bar))  'The expression (Bar) is evaluated and 
                     '  the resultant value 0 is passed ByVal to Foo
    Debug.Print Bar '--> 0
End Sub
忆离笙 2024-10-22 08:44:40

应该是:

 AddNr ana

即没有括号。

来自微软帮助:

备注

您无需使用呼叫
调用过程时的关键字。
但是,如果您使用 Call 关键字
调用一个需要的过程
参数、参数列表必须是
括在括号中。如果你省略
Call 关键字,您还必须省略
参数列表周围的括号。
如果您使用任一 Call 语法来调用
任何固有的或用户定义的
函数,函数的返回值
被丢弃。

That should be:

 AddNr ana

That is, no brackets.

From Microsoft Help:

Remarks

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文