简体   繁体   English

我可以使用SUMPRODUCT来做到这一点吗?

[英]Can I use SUMPRODUCT to accomplish this?

Need to sum a range based on if a value is in a column and one of a set of values is in another column, or vice versa. 需要根据一个值是否在一列中以及一组值中的一个是否在另一列中来对范围求和,反之亦然。 eg I have The following table: 例如我有下表:

A     B      C      D  
M     C      C      1
F     C      C      2
S     N      C      3
S     N      N      4
M     -      C      5
N     C      C      6
M     C      N      7

If (Column A contains "M" or "S") AND ((Column B contains "C" AND Column C Contains "C" Or "N" Or "-") OR (Column C contains "C" AND Column B Contains "C" Or "N" Or "-")) Then Sum column D 如果(A列包含“ M”或“ S”)AND((B列包含“ C”且C列包含“ C”或“ N”或“-”)或(C列包含“ C”且B列包含“ C”或“ N”或“-”))然后求和D列

So from my table my results would be 1 + 3 + 5 + 7 = 16 所以从我的表中我的结果将是1 + 3 + 5 + 7 = 16

I would add a fifth column with a condition for the current line returning the value in D if all conditions are true or 0 otherwise. 如果所有条件都为true,我将添加第五列,其中包含当前行的条件,以返回D的值;否则,返回0

=Iif(AND(Or($A1 = "M", $A1 = "S"),OR(AND($B1 = "C",Or($C1 = "C",$C1 = "N",$C1 = "-")), AND($C1 = "C",OR($B1 = "C",$B1 = "N",$B1 = "-")))),$D1,0)

Then in a cell somewhere write =sum($E:$E) . 然后在某个位置的单元格中写入=sum($E:$E) With your example, I get 16, as intended. 以您的示例为例,我得到16。

You can use SUMPRODUCT like this: 您可以像这样使用SUMPRODUCT

=SUMPRODUCT(ISNUMBER(MATCH(A2:A10,{"M","S"},0)*MATCH(B2:B10&"^"&C2:C10,{"C^C","C^N","C^-","N^C","-^C"},0))+0,D2:D10)

MATCH is used to check for both valid possibilities in column A and then all 5 possibilities for concatenated columns B and C - if those conditions are met then column D will be summed. MATCH用于检查A列中的两个有效可能性,然后检查并置的B和C列的所有5种可能性-如果满足这些条件,则将对D列求和。 Extend column ranges as required but preferably don't use whole columns 根据需要扩展列范围,但最好不要使用整个列

.....or shorter with SUMIFS like this: .....或更短的SUMIFS像这样:

=SUM(SUMIFS(D:D,A:A,{"M";"S"},B:B,{"C","C","C","N","-"},C:C,{"C","N","-","C","C"}))

For that version you can use whole columns with no loss of efficiency. 对于该版本,您可以使用整个列而不会损失效率。

Note that in this version all the separators in the array constants are commas EXCEPT for the semi-colon in {"M";"S"} which needs to be that way 请注意,在此版本中,数组常量中的所有分隔符都是逗号, 除了 {“ M”;“ S”}中的分号外,需要这样

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM