Posted on October 22, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| 'Arrays, Collections, Lists - How to sum all the elements in an two dimensional array
Function ArraySum2(arr As Variant) As Variant
Dim I As Long, j As Long, result As Variant
' First check whether we can really work with this array.
Select Case NumberOfDims(arr)
Case 1 ' One-dimensional array
For I = LBound(arr) To UBound(arr)
result = result + arr(I)
Next
Case 2 ' Two-dimensional array
For I = LBound(arr) To UBound(arr)
For j = LBound(arr, 2) To UBound(arr, 2)
result = result + arr(I, j)
Next
Next
Case Else ' Not an array, or too many dimensions
Err.Raise 1001, , "Not an array or more than two dimensions"
End Select
ArraySum2 = result
End Function |