2
我在一個程序中創建了一個數組,這是最終的結果,我想在新程序中將這些結果用作計算的輸入。有沒有辦法將多維數組作爲參數傳遞給另一個過程?你如何將它放入調用中,以及如何在將要使用的過程中定義參數?是否可以在EXCEL VBA中將多維數組作爲參數傳遞?
謝謝
我在一個程序中創建了一個數組,這是最終的結果,我想在新程序中將這些結果用作計算的輸入。有沒有辦法將多維數組作爲參數傳遞給另一個過程?你如何將它放入調用中,以及如何在將要使用的過程中定義參數?是否可以在EXCEL VBA中將多維數組作爲參數傳遞?
謝謝
這是你之後的事情嗎?
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
傳遞給你一個單維數組,通過用括號裝飾例程參數;
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