3

我在一个过程中创建了一个数组,它是最终结果,我想在新过程中利用这些结果作为计算的输入。有没有办法将多维数组作为参数传递给另一个过程?您将如何将其放入调用中,以及如何在将使用它的过程中定义参数?

谢谢

4

2 回答 2

3

通过用括号装饰常规参数来传递单维数组;

sub a()
    dim x(1, 1) As long
    x(0, 0) = 1
    x(1, 1) = 4
    process x
end sub

sub process(arr() As long)
    Msgbox arr(0, 0)
    Msgbox arr(1, 1)
end sub
于 2012-12-18T12:33:36.977 回答
0

Is this the sort of thing you are after?

Sub AAATest()
''''''''''''''''''''''''
' Dynamic array to hold
' the result.
''''''''''''''''''''''''
Dim ReturnArr() As Long
Dim Ndx1 As Long
Dim Ndx2 As Long
Dim NumDims As Long
''''''''''''''''''''''''''
' call the function to get
' the result array.
''''''''''''''''''''''''''
ReturnArr = ReturnMulti()
NumDims = NumberOfArrayDimensions(Arr:=ReturnArr)
Select Case NumDims
    Case 0
        '''''''''''''''''''
        ' unallocated array
        '''''''''''''''''''
    Case 1
        ''''''''''''''''''''''''''
        ' single dimensional array
        ''''''''''''''''''''''''''
        For Ndx1 = LBound(ReturnArr) To UBound(ReturnArr)
            Debug.Print ReturnArr(Ndx1)
        Next Ndx1
    Case 2
        '''''''''''''''''''''''''''
        ' two dimensional array
        '''''''''''''''''''''''''''
        For Ndx1 = LBound(ReturnArr, 1) To UBound(ReturnArr, 1)
            For Ndx2 = LBound(ReturnArr, 2) To UBound(ReturnArr, 2)
                Debug.Print ReturnArr(Ndx1, Ndx2)
            Next Ndx2
        Next Ndx1
    Case Else
        ''''''''''''''''''''''
        ' too many dimensions
        ''''''''''''''''''''''
End Select
End Sub


Function ReturnMulti() As Long()
''''''''''''''''''''''''''''''''''''
' Returns a mutli-dimensional array.
''''''''''''''''''''''''''''''''''''
Dim A(1 To 2, 1 To 3) As Long
'''''''''''''''''''''''''''''
' put in some values.
'''''''''''''''''''''''''''''
A(1, 1) = 100
A(1, 2) = 200
A(1, 3) = 300
A(2, 1) = 400
A(2, 2) = 500
A(2, 3) = 600
ReturnMulti = A()
End Function
于 2012-12-18T12:32:08.730 回答