Pspl
Pspl

Reputation: 1474

Sum a specific range of elements of a multi-dimension array

How can I sum a specific range of elements of a multi-dimension array without using a For... Nextstatement?

I know how to do it with a 1-dimension array. For example, the next code:

Dim GRMORT(1 to 864) As Double
Dim PSUM as Double
PSUM = Application.Sum(Application.Index(GRMORT, 1, Evaluate("ROW(100:200")")))

return the sum of the 100th to 200th elements. However, if I use a 2-dimension array:

Dim GRMORT(1 to 864, 1 to 24) As Double

the function doesn't work. I thought it had to be with the second argument of the Index function, so I tried change it to 2, 3, ... But that wasn't the solution.

How can I change the Indexfunction to get the sum of the 100th to 200th elements of the fourth column (or any other column)?

Upvotes: 0

Views: 373

Answers (1)

Rory
Rory

Reputation: 34055

Just supply the column argument:

Dim GRMORT(1 To 864, 1 To 5) As Double
Dim PSUM As Double
PSUM = Application.Sum(Application.Index(GRMORT, Evaluate("ROW(100:200)"), 4))

Upvotes: 3

Related Questions