9

问题定义:

在单元格中输入任意数字A1。现在在第一行的任何地方尝试以下公式。

=SUM(INDIRECT("A"&ROW()))

=SUMPRODUCT(INDIRECT("A"&ROW()))

第一个公式计算,第二个给出#VALUE 错误。这是由于ROW()函数内部的行为不同造成的SUMPRODUCT()

在第一个公式中,ROW()返回1。在第二个公式中,行返回{1}(一个长度的数组),即使该公式尚未作为 CSE 公式输入。

为什么会这样?

背景

我需要评估类型的公式

=SUMPRODUCT(INDIRECT(*range formed by concatenation and using ROW()*)>1)

这是一个错误。作为此问题的解决方法,我现在ROW()在另一个单元格中计算(显然在同一行中)并将其连接到我的INDIRECT(). 或者,我也尝试将其封装在 sum 函数中,例如SUM(ROW()),并且效果也很好。

如果有人可以解释(或指向可以解释的资源)为什么在没有输入 CSE的情况下ROW()返回内部数组,我将不胜感激。SUMPRODUCT()

4

3 回答 3

7

Interesting question. There are subtle issues here which I haven't seen documented.

It seems INDIRECT("A"&ROW()) returns an array consisting of one element which is a reference to a cell - not the value in that cell. Many functions cannot resolve this type of data correctly but a few functions such as N and T can "dereference" the array and return the underlying value.

Take this case where there are two elements in the array:

=SUM(N(INDIRECT("A"&ROW(1:2))))

This returns A1+A2 when array entered but it only returns A1 when entered normally. However changing ROW(1:2) to {1;2} in this formula returns the correct result when entered normally. The equivalent SUMPRODUCT formula returns A1+A2 whether array entered or not.

This may be related to how the arguments are registered in the function. According to http://msdn.microsoft.com/en-us/library/bb687900.aspx there are essentially two methods to register function arguments to handle Excel data types:

Type R/U: "Values, arrays, and range references."

Type P/Q: "Excel converts single-cell references to simple values and multi-cell references to arrays when preparing these arguments."

SUM arguments seem to conform with type R/U while SUMPRODUCT arguments behave like type P/Q. Array-entering the SUM formula above forces the range reference argument in ROW to be evaluated as an array whereas this happens automatically with SUMPRODUCT.

Update

After a little more investigation, here's further evidence that might support this theory. Based on the link in the comment, the formula =SUM((A1,A2)) gives the same values as:

?executeexcel4macro("CALL(""Xlcall32"",""Excel4"",""2JRJR"",4,,1,(!R1C1,!R2C1))")

Registering the last argument as type P by changing 2JRJR to 2JRJP gives an error in this case but does allow for single area ranges like !R1C1:!R2C1. On the other hand, changing the 4 (xlfsum) to 228 (xlfsumproduct) only allows single area references either way it's called just like SUMPRODUCT.

于 2012-06-20T21:18:58.917 回答
2

作为ROW()返回一个数组,用于INDEX获取第一个元素。

你的例子然后变成:=SUMPRODUCT(INDIRECT("A"&INDEX(ROW(),1)))

于 2012-06-20T17:06:55.227 回答
1

我不认为 ROW() 在这里表现不同,它在两种情况下都返回一个数组。我假设 SUM 和 SUMPRODUCT 以不同的方式处理该数组 - 不知道为什么。

Many functions or combinations of them return arrays - you don't need CTRL+SHIFT+ENTER to make that happen, you only need CSE in many cases to process the arrays created.

I would just use INDEX in place of INDIRECT (which also benefits you by avoiding a volatile function), i.e.

=SUMPRODUCT(INDEX(A:A,ROW()))

....expanding that to your range this formula will count the number of values > 1 in a range in column A where x defines the start row and y the end row

=COUNTIF(INDEX(A:A,x):INDEX(A:A,y),">1")

x and y can be calculated by formulas

you can use SUMPRODUCT or COUNTIFS in a similar way if there are more conditions to add

于 2012-06-20T17:39:16.773 回答