View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Sum Column in Array

You could let excel help by using =sum() and =index(). Or you could loop
through that 3rd column and add each element to a total.

Option Explicit
Sub testme()

Dim myArr(1 To 10, 1 To 10) As Double

Dim myTotal As Double
Dim rCtr As Double
Dim cCtr As Double
Dim myTestTotal As Double

'just some test data
For rCtr = LBound(myArr, 1) To UBound(myArr, 1)
For cCtr = LBound(myArr, 2) To UBound(myArr, 2)
myArr(rCtr, cCtr) = rCtr / cCtr
Next cCtr
Next rCtr

'let excel help
With Application
myTotal = .Sum(.Index(myArr, , 3))
End With

'or loop
myTestTotal = 0
For rCtr = LBound(myArr, 1) To UBound(myArr, 1)
myTestTotal = myTestTotal + myArr(rCtr, 3)
Next rCtr

MsgBox myTotal & vbLf & myTestTotal

End Sub

Troubled User wrote:

I have a two dimensional array (10,10) and want to sum the third column
without creating a new array. I have tried a variety of different methods
but can't seem to get it. Any help is much appreciated.


--

Dave Peterson