简体   繁体   English

excel中基于数组的sumif公式

[英]array based sumif in excel by formula

So I m trying to find some alternative to sumifs in excel where each condition needs to be checked in a 2D range instead of a 1D range.所以我试图在 excel 中找到一些 sumifs 的替代方法,其中每个条件都需要在 2D 范围而不是 1D 范围内检查。

For example, in the below table I want the sum of values in column V for rows where A12 ("IJ") is present in range A2:C8 (P), B12 ("NM") is present in the range D2:F8 (S) and C12 ("XX") is present in range G2:I8 (A)例如,在下表中,我想要 V 列中 A12(“IJ”)存在于范围 A2:C8(P)、B12(“NM”)存在于范围 D2:F8 中的行的值的总和(S) 和 C12 (“XX”) 存在于 G2:I8 (A) 范围内

在此处输入图片说明

I am trying to find a solution involving an array-based formula (without VBA).我试图找到一个涉及基于数组的公式(没有 VBA)的解决方案。 Like for example in the below-given formulas, SUMPRODUCT((B2:B8'=A12)*J2:J8) will give an array-based calculation as follows例如在下面给出的公式中, SUMPRODUCT((B2:B8'=A12)*J2:J8)将给出一个基于数组的计算,如下所示

SUMPRODUCT({TRUE;FALSE;TRUE;FALSE;FALSE;TRUE;FALSE}*{22;79;45;67;43;72;52}) 
= SUMPRODUCT({22;0;45;0;0;72;0})
=139

It is easy when there is only one condition needs to be checked but like sumifs, I intend to check multiple conditions, but as soon as I add other conditions, the array becomes multidimensional and gives the wrong answer.当只需要检查一个条件时很容易,但像 sumifs 一样,我打算检查多个条件,但是一旦我添加其他条件,数组就会变成多维并给出错误的答案。

Example:例子:

SUMPRODUCT((A2:C8=A12)*(D2:F8=B12)*J2:J8) breaks down to SUMPRODUCT((A2:C8=A12)*(D2:F8=B12)*J2:J8)分解为

=SUMPRODUCT(
{FALSE,TRUE,FALSE;FALSE,FALSE,FALSE;FALSE,TRUE,FALSE;FALSE,FALSE,FALSE;FALSE,FALSE,FALSE;FALSE,TRUE,FALSE;FALSE,FALSE,FALSE}*
{TRUE,FALSE,FALSE;FALSE,FALSE,FALSE;TRUE,FALSE,FALSE;FALSE,FALSE,FALSE;FALSE,FALSE,FALSE;TRUE,FALSE,FALSE;FALSE,FALSE,FALSE}
*J2:J8)  

in the background what is happening is (example for 3rd row)在后台发生的事情是(例如第三行)

SUMPRODUCT( ({FALSE, TRUE ,FALSE} * {TRUE,FALSE,FALSE}) * 45 )

= SUMPRODUCT({FALSE,FALSE,FALSE} *45 )

=0


SUMPRODUCT(({FALSE,TRUE ,FALSE} + {TRUE,FALSE,FALSE}) * 45 )

= SUMPRODUCT({TRUE,TRUE,FALSE} *45 )

= 90

#expected answer =45

Can someone help me understand where I am going wrong or what I am missing?有人可以帮助我了解我哪里出错了或我遗漏了什么吗?

If there is any other way then suggestions are always welcome.如果有任何其他方式,那么建议总是受欢迎的。

Please note this is a dummy data actual data is very big for each header (P,S,A) there are values in 10 columns respectively and the number of rows is also very large.请注意,这是一个虚拟数据,每个标题(P,S,A)的实际数据非常大,分别有10列中的值,行数也非常大。

Try this...尝试这个...

=SUMPRODUCT( ((A2:A8=A12)+(B2:B8=A12)+(C2:C8=A12)) * ((D2:D8=B12)+(E2:E8=B12)+(F2:F8=B12)) * ((G2:G8=C12)+(H2:H8=C12)+(I2:I8=C12)) * J2:J8 )

For SUMPRODUCT to work, the shape of the Boolean array needs to match the shape of the array you wish to conditionally sum.要使 SUMPRODUCT 起作用,布尔数组的形状需要与您希望有条件地求和的数组的形状相匹配。

J2:J8 is seven rows tall by one column wide. J2:J8是七行高一列宽。

The above formula creates an array of 1s and 0s from your three criteria ranges and shapes it into seven rows tall by one column wide.上面的公式根据您的三个条件范围创建了一个由 1 和 0 组成的数组,并将其整形为七行高一列宽。

At that point, SUMPRODUCT can do it's normal thing because the criteria array matches the dimension of the sum array J2:J8 .在这一点上, SUMPRODUCT 可以做它正常的事情,因为标准数组匹配和数组J2:J8的维度。

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

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