Let's say that we have a 4x5 array. How can I sum get a separate 1D array with 5 elements where every element represents the column-sum of elements from the first array?
Asked
Active
Viewed 9,840 times
3 Answers
1
The code sums the column of YourArray; for sum of the row switch 0 and 1
With Application.WorksheetFunction
sum_array = .sum(.Index(YourArray(), 0, 1)) 'switch 0 and 1 for row
End With

tom
- 11
- 2
0
If done in the spreadsheet
I think I understand your question. At the bottom of each column simply put a SUM equation and sum over only the numbers in that column. For example if your array is in A1:E4 (first five columns and first four rows) put the following in cell A5:
=SUM(A1:A4)
And then copy cell A5 into cells B5 to E5.
EDIT - If done in VBA
Dim Arr_1(1 to 4, 1 to 5) as Double
Dim Arr_2(1 to 5) as Double
For col = 1 to 5
Arr_2(col) = 0
for row = 1 to 4
Arr_2(col) = Arr_2(col) + Arr_1(row, col)
Next row
Next col

user1228123
- 424
- 4
- 15
-
-
Yes, you understand it fairly rigtht. But the thing is that I want to deal only with arrays, no cell values. So the 2D array is just an array and I want to make a new array (not written in cells) with the sums – Matthew Apr 14 '18 at 13:47
-
0
Dim Arr_1(1 to 4, 1 to 5) as Double
Dim Arr_2(1 to 5) as Double
For col = 1 to 5
Arr_2(col) = 0
for row = 1 to 4
Arr_2(col) = Arr_2(col) + Arr_1(row, col)
Next row
Next col
Thank you, user1228123!

Matthew
- 15
- 1
- 5