如何区分sumproduct是否需要用CSE插入?

How to distinguish if sumproduct needs to be inserted with CSE or not?

我有时会使用 sumproduct 的固有数组功能来避免必须使用 Control + Shift + Enter 输入公式。但它并不总是有效。例如

=SUMPRODUCT((LEN(B2:F2)-LEN(SUBSTITUTE(B2:F2,M:M,"")))*N:N)

会起作用,而

=SUMPRODUCT(--IF(ISNUMBER(N6:N9),N6:N9))

不会。

我不是很清楚为什么第一个给出正确的结果而第二个没有。

问得好。我的经验法则 > 你看到 IF 和一个要分析的范围 > 你按 CSE。

为什么?有些函数会为您本地处理 CSE(SUMPRODUCT 是其中之一),但其他函数则不会,例如 SUM 但肯定也会 IF。看看here and here. Bottom line of the theory (AFAIK) is that CSE will disable something called "implicit intersection" which is explained here。归结为:

"Implicit intersection occurs when a range is passed to a function that expects a scalar (single) value. In this situation, Excel will try to resolve the formula using a reference in the same row, or in the same column......Entering an array formula with Control + Shift + Enter (CSE) explicitly disables the implicit intersection behavior. This makes it possible to create formulas that manipulate multiple values input as ranges."

因为您使用 IF,所以它在 SUMPRODUCT 内并不重要。您仍然需要按 CSE 来禁用使用 IF.

附带的本机 "implicit intersection"

FWIW:有关称为 "implicit intersection".

的行为的一些附加信息

让我们想象一下以下数据:

我从范围 A2:C2 创建了一个名为 Vals 的命名范围。现在 B5 中的公式只是 =Vals 但结果是 Val5。意思是隐式交集return编辑了我命名范围中与我输入公式的列相交的值。

为什么?因为在后台(看不见)Excel 使用隐式交集运算符 ("@") 来 return 来自刚才提到的交集的单个值。我会使用 CSE(读取,删除逻辑运算符),值 returned 将是 Val2(数组中左上角的值)。

"Implicit intersection logic reduces many values to a single value. Excel did this to force a formula to return a single value, since a cell could only contain a single value."

逻辑运算符“@”将阻止数组的 return 并确保您将获得单个值 returned。删除这个逻辑运算符(这是我们通过按 CSE 或使用本机执行此操作的函数所做的)将使公式 return 成为数组。

您可能 see/know 不了解此运算符,但随着动态数组公式的出现,它们将更多地出现在您的公式中。请参阅 this MS-documentation on the matter. With those new functionalities, removing the logical operator will not only return the array, it will actually spill the values to neighboring cells. Hence the term "Dynamic array formulas". So you can see the new dynamic array formulas as an automated alternative 了解遗留的 CSE 公式,并添加了溢出功能。


所以总结一下:

你的第二个公式也可以写成:

=@SUMPRODUCT(--@IF(@ISNUMBER(N6:N9),N6:N9))

Enter 不起作用,因为只有 SUMPRODUCT 本身取消了(看不见的)逻辑运算符,而 IF 只需要一个标量(单个)价值。所以,看不见但有效,你的公式看起来像:

=SUMPRODUCT(--@IF(@ISNUMBER(N6:N9),N6:N9))

但是,按Control + Shift + Enter确实会排除逻辑运算符并有效地使您的公式看起来像:

=SUMPRODUCT(--IF(ISNUMBER(N6:N9),N6:N9))

因此能够使用数组。希望这澄清了为什么您需要使用第二个 IF 公式按 CSE。


有趣的事实: 下次尝试写=@SUMPRODUCT(...=@IF(...。您会注意到公式已被接受,但逻辑运算符消失了。后台使用此运算符的标志 =)