how to sum a range into an array?

K

Khaki

Hi
I would like to know how to sum a range of cells and put the resul
into an array

Say my range is from B2 to G5
and I have an array of size 6,
myArray(6) as double

is there a function to sum a range, and return the results int
myArray?

Another part of the question is is there a function to do a cumulativ
sum of a range or do I have to cycle thru each cell?

Basically is there a better way to write this (code below) that i
optimized for speed?

dim myArray(6) as double
for i = 0 to 5 do
myArray(i) = myArray(i) + cell(2,i + 1)
next

Thanks
 
F

Frank Kabel

Hi
try the following
Sub foo()
Dim ret_arr(6) As Double
Dim i As Integer
For i = 1 To 6
ret_arr(i) = Application.WorksheetFunction. _
sum(Range(Cells(2, i + 1), Cells(5, i + 1)))
Next
For i = 1 To 6
MsgBox ret_arr(i)
Next
End Sub
 
T

Tom Ogilvy

dim myArray(0 to 5) as double
MyArray(0) = Cells(2,1)
for i = 1 to 5 do
myArray(i) = myArray(i-1) + cells(2,i + 1)
next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top