Home > How-To Library > Arrays, Collections, Lists

How to sum all the elements in an two dimensional array

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
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

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.