使用数组和INDEX求和值 [英] Sum values using Arrays and INDEX
问题描述
我有以下样本表:
1/A B C D E F G H I J
2
3 Points 8 4 2 1
4
5 Values 1 2 3 4 4 3 1 2
我试图根据值中的数组索引来计算点数。
I'm trying to sum the 'Points' based upon the array index from the 'Values'.
我的预期结果是: 30
这是我的公式:
{=SUM(INDEX($C$3:$F$3,1,C5:J5))}
由于某些原因,这只会返回数组的第一个值,而不是整个数值。
For some reason though, this only returns the first value of the array, rather than the entire sum.
为了澄清,C#版本将是这样的:
To clarify, the C# version would be something like:
var points = new int[] { 8, 4, 2, 1 };
var values = new int[] { 2, 4, 3, 1, 2, 4, 2 };
var result = (from v in values
select points[v - 1]).Sum(); // -1 as '4' will crash, but in Excel '4' is fine
编辑:添加进一步的澄清示例
另一个例子来澄清:
。 值表示数组的索引。
Points is the array. The 'values' represents the index of the array to sum.
上面的示例与以下相同:
The example above is the same as:
=SUM(8, 4, 2, 1, 1, 2, 8, 4)
推荐答案
INDEX
永远不会从数组中获取其行或列参数,然后在一个数组中执行多次公式包含在一个单元格中。
INDEX
will never take its row or column parameters from arrays and then perform multiple times within one array formula contained in one cell. For this OFFSET
will be needed.
{=SUM(N(OFFSET($C$3,,C5:J5-1)))}
作为数组公式。
或
=SUMPRODUCT(N(OFFSET($C$3,,C5:J5-1)))
作为隐式数组公式,不需要[Ctrl] + [Shift] + [Enter]。
as an implicit array formula without the need for [Ctrl]+[Shift]+[Enter].
这篇关于使用数组和INDEX求和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!