Excel VBA-2D数组中的行/列总和 [英] Excel VBA - Sum rows/columns in a 2D array

查看:175
本文介绍了Excel VBA-2D数组中的行/列总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个4x5的阵列.如何求和得到一个包含5个元素的单独一维数组,其中每个元素代表第一个数组中元素的列总和?

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?

推荐答案

如果在电子表格中完成

我想我理解你的问题.在每列的底部,只需简单地添加一个SUM公式,然后仅对该列中的数字求和.例如,如果您的数组位于A1:E4(前五列和前四行)中,则将以下内容放入单元格A5中:

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)

然后将单元格A5复制到单元格B5至E5.

And then copy cell A5 into cells B5 to E5.

编辑-如果在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

这篇关于Excel VBA-2D数组中的行/列总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆