VBA array syntax

时间:2015-10-29 15:44:08

标签: vba

Looking over vba arrays and stumbled upon something and need someone to clear it up.

Sub AAATest()
Dim StaticArray(1 To 3) As Long
Dim N As Long
StaticArray(1) = 1
StaticArray(2) = 2
StaticArray(3) = 3
PopulatePassedArray Arr:=StaticArray
For N = LBound(StaticArray) To UBound(StaticArray)
    Debug.Print StaticArray(N)
Next N
End Sub

AND

Sub PopulatePassedArray(ByRef Arr() As Long)
''''''''''''''''''''''''''''''''''''
' PopulatePassedArray
' This puts some values in Arr.
''''''''''''''''''''''''''''''''''''
Dim N As Long
For N = LBound(Arr) To UBound(Arr)
    Arr(N) = N * 10
Next N
End Sub

What's happening at

PopulatePassedArray Arr:=StaticArray

in AAATest sub

1 个答案:

答案 0 :(得分:1)

有两种方法可以将参数传递给另一个过程:使用命名参数或按顺序。按顺序传递它们时,必须按照与过程定义相同的顺序通过它们。

Function DoTheThing(arg1 As Double, arg2 As String, arg3 As Boolean) As Double

当您调用此功能(按顺序)时,您可以将其称为

x = DoTheThing(.01, "SomeString", TRUE)

使用命名参数调用函数时,可以使用:=,参数名称和参数值。 :=不是一个特殊的赋值运算符 - 我猜它有点像。结果是,当您使用命名参数时,您可以按任何顺序提供它们。

x = DoTheThing(arg2:="SomeString", arg3:=TRUE, arg1:=.01)

有些人还认为命名参数使您的代码更具可读性。我不是那些人之一。它会使它变得混乱,如果你传递超过两三个论点,你无论如何都会做错。