避免循环通过VBA数组

时间:2014-07-24 14:50:13

标签: arrays excel vba loops

使用以下代码(http://www.cpearson.com/Excel/PassingAndReturningArrays.htm)有一种方法可以避免两个函数的循环:

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

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

例如,我希望它看起来像这样:

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
    Debug.Print StaticArray(:,1) 

End Sub

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

但显然后面的代码不能编译。有什么想法吗?

0 个答案:

没有答案