VBA 通过引用传递数组并修改内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28326429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Pass Array By Reference and Modify Contents
提问by Geoffrey
VBA question. I'm using it to make Solidworks macros, but that's not important.
VBA 问题。我用它来制作 Solidworks 宏,但这并不重要。
Can someone explain the syntax to pass an array (1-Dimensional of type Double, with length of three) to a subroutine or function so I can modify the contents. I know that they have to be passed by reference. I'm talking exact syntax, because everything I try I get type mismatch error on the subroutine/function call.
有人可以解释将数组(Double 类型的一维,长度为三)传递给子例程或函数的语法,以便我可以修改内容。我知道它们必须通过引用传递。我说的是确切的语法,因为我尝试的所有操作都会在子例程/函数调用中遇到类型不匹配错误。
All I'm looking for is the correct Dim statement for the array of Doubles, initialize statement to make all array values zero, then the subroutine call statement and subroutine header that I would need. Please be specific if I need to use variant type or dynamic array, even when I already know the type and size of the array. Use a function or a sub, I don't care which.
我正在寻找的是双精度数组的正确 Dim 语句,初始化语句以使所有数组值为零,然后是我需要的子例程调用语句和子例程标头。如果我需要使用变体类型或动态数组,请具体说明,即使我已经知道数组的类型和大小。使用函数或子函数,我不在乎哪个。
My code works fine as of now, but I'm tired of avoiding function and subroutine calls when I'm using arrays. I've looked at countless documentation and similar questions on here, but I just cannot figure it out. Thanks a lot.
我的代码现在运行良好,但我厌倦了在使用数组时避免函数和子例程调用。我在这里查看了无数文档和类似问题,但我无法弄清楚。非常感谢。
回答by David Zemens
This is fairly trivial, using the ByRef
keyword in the function signature will pass your array by reference rather than by value. This means that manipulations tothat array will be preserved and bubble up to the calling scope. This is probably an oversimplification, but think of it this way:
这是相当简单的,ByRef
在函数签名中使用关键字将通过引用而不是值传递您的数组。这意味着,操作于该阵列将被保留和冒泡到调用范围。这可能过于简单化了,但可以这样想:
ByRef
: you're passing a reference to the thing(array, Object, etc.) and any transformations to that thingwill be apparent anywhere that thingexists.ByVal
: you're passing the valuerepresentation of the thing. Essentially, you're passing a "copy" of it. You can manipulate this object, but it is notthe same object from the calling scope, it's just a copy. So when the enclosing scope ends, you're still left with only the original thing.
ByRef
:你传递一个参考的东西(数组,对象等)和任何转换的东西将是显而易见的任何地方,东西存在。ByVal
:您正在传递事物的值表示。本质上,您正在传递它的“副本”。你可以操纵这个对象,但它是不是从主叫范围相同的对象,它只是一个副本。因此,当封闭范围结束时,您仍然只剩下原始内容。
Initialize your array as a numeric type and that will give you default 0
values.
将您的数组初始化为数字类型,这将为您提供默认0
值。
Example as follows:
示例如下:
Option Explicit
Sub foo()
Dim myArray(1 To 3) As Double 'The array is initialized with zero-values
Call bar(myArray)
MsgBox myArray(3)
End Sub
Sub bar(ByRef dblArray() As Double)
dblArray(1) = 123
dblArray(2) = 345
dblArray(3) = 33456
End Sub