简体   繁体   English

Excel,Sumproduct,{}中的多个条件搜索

[英]Excel, Sumproduct, multiple conditions search in {}

I run into several postings on the internet (incl. stackowerflow) with code like this 我在互联网(包括stackowerflow)上遇到了几条这样的代码

=SUMPRODUCT((A1:A10="Marketing")*(B1:B10={"North","South"})*(C1:C10))

Conditions for search are neatly put into {} . 搜索条件整齐地放在{} I have 28 such conditions to search for, so I'm looking for a way to make the formula easier to read. 我要搜索28种这样的条件,因此我正在寻找一种使公式更易于阅读的方法。 If I try it, i get N/A. 如果尝试,我会得到N / A。

Is there a trick I'm missing? 我有想念的招吗?

I'm aware that it can be written (B1:B10="North") + (B1:B10="South") but with 28 items it is going to be long. 我知道可以将其写为(B1:B10="North") + (B1:B10="South")但要包含28个项目,它将变得很长。

Thank you in advance 先感谢您

EDIT1: (Disregard) EDIT1 :(忽略)

Tried Axel's suggestion 尝试了Axel的建议

Simple example 简单的例子

        -   A   B   C   D
        1       1   2   3
        2   1   2   2   3
        3   2   4   4   6
        4   3   6   6   9



 =SUMPRODUCT((A2:A4={2,3})*(B2:D4))

    Returns Sumproduct(({1,2,3}={2,3})*(B2:D4)) -> I still get N/A for last column when you continue in process

    Same for  
    =SUMPRODUCT((A2:A4=A6:B6)*(B2:D4))
    where A6:B6 is list of conditions

    or 

    =SUMPRODUCT((A2:A4=testrange)*(B2:D4))

I'm trying to put all conditions within formula {"case1","case2",...} and so but can't make it work. 我试图将所有条件都放在公式{"case1","case2",...} ,但无法使其正常工作。

Edit 2: Ok, I see the difference now. 编辑2:好的,我现在看到了区别。 Initial formula is column by column by column 初始公式逐列

What I'm trying to solve 我要解决的问题

Column A- list of accounts, I need to find 28 of them 
Row 1 - months (conditions varies)
Range B2:AA462 - values 

I can write it all with (A2:A462="account1")+(A2:A462="acount2")... up to 28 cases, but I'm asking whether there is a way to write it more simpler 我可以用(A2:A462="account1")+(A2:A462="acount2")...最多写28种情况,但是我在问是否有一种更简单的书写方法

Something like initial A2:A462={"North","South"} 类似于初始A2:A462={"North","South"}

Something like 就像是

=Sumproduct((A2:A462={"account1","account2",...})*(B1:AA1="June")*(B2:AA462))

Is there a way write this somehow? 有什么办法可以这样写吗?

EDIT 4: 编辑4:

Few weeks later inspired by Axel's inputs 几周后,受到Axel的投入启发

=SUMPRODUCT(MMULT(--(A2:A7=G1:J1),ROW(1:4)/ROW(1:4))*(B1:E1=G4)*B2:E7)

Can be grown into 可以长成

{=SUMPRODUCT(MMULT(--(A2:A7=TRANSPOSE(namedrange)),ROW(OFFSET(A1,0,0,COUNTA(namedrange)))/ROW(OFFSET(A1,0,0,COUNTA(namedrange))))*(B1:E1=G4)*(B2:E7))}

Ok, named range, has conditions within column, more natural way to keep a list of conditions you want to filter for. 好的,命名范围是在列中包含条件,这是保留要过滤条件的列表的更自然的方式。 Also MMULT is now flexible, and counts number of conditions and adjust number of rows to multiply by. MMULT现在也很灵活,它可以计算条件数并调整要乘以的行数。 Whole formula must be entered as array formula. 整个公式必须作为数组公式输入。

{"North","South"} is the array literal for a row vector. {"North","South"}是行向量的数组文字。 That means it is as if "North" and "South" is placed in juxtaposed cells in one row. 这意味着好像“北”和“南”被放置在并排的单元格中。 So if "North" is in E1 and "South" is in F1 , then the formula could also be: 因此,如果“ North”在E1而“ South”在F1 ,则公式也可以是:

=SUMPRODUCT((A1:A13="Marketing")*(B1:B13=E1:F1)*C1:C13)

With more criteria it could be: 使用更多标准,可能是:

=SUMPRODUCT((A1:A13="Marketing")*(B1:B13=E1:H1)*C1:C13)

It is important that the criterias are in a row vector (one row, multiple columns) since the B1:B13 is a column vector. 由于B1:B13是列向量,因此准则必须位于行向量(一行,多列)中,这一点很重要。


Answer to your Edit 2: 回答您的编辑2:

The approach: 该方法:

=SUMPRODUCT(((A2:A462="account1")+(A2:A462="account2")+...+(A2:A462="account28"))*(B1:AA1="June")*B2:AA462)

, which will work, is different from (A2:A462={"account1","account2",...,"account28"}) . ,它将起作用(A2:A462={"account1","account2",...,"account28"}) The latter cannot work since it creates a matrix of 461 rows and 28 columns while the working one ((A2:A462="account1")+(A2:A462="account2")+...+(A2:A462="account28")) is only a vector of 461 rows in one column. 后者无法工作,因为它创建了一个461行28列的矩阵,而工作的((A2:A462="account1")+(A2:A462="account2")+...+(A2:A462="account28"))仅是一列中461行的向量。

The equivalent could be: 等效项可能是:

=SUMPRODUCT(MMULT(--(A2:A462={"account1","account2",...,"account28"});ROW(1:28)/ROW(1:28))*(B1:AA1="June")*B2:AA462)

and if "account1","account2",...,"account28" are in AC1:BD1 then also: 如果“ account1”,“ account2”,...,“ account28”位于AC1:BD1则:

=SUMPRODUCT(MMULT(--(A2:A462=AC1:BD1);ROW(1:28)/ROW(1:28))*(B1:AA1="June")*B2:AA462)

What is this doing? 这是在做什么 It uses MMULT to transform the matrix of 461 rows and 28 columns into a vector of 461 rows by multiplying the matrix with a row vector of 28 rows filled with 1. 它使用MMULT通过将矩阵与28行的行向量乘以1来将461行和28列的矩阵转换为461行的向量。

So if there is a 1 in one of the 28 columns in each row of the matrix, then there will also be a 1 as the row value of the resulting vector of 461 rows. 因此,如果矩阵每一行的28列之一中有1,那么作为结果的461行向量的行值也将有1。

Example: 例:

在此处输入图片说明

Formula in H3 : H3公式:

=SUMPRODUCT(((A2:A7=G1)+(A2:A7=H1)+(A2:A7=I1)+(A2:A7=J1))*(B1:E1=G3)*B2:E7)

Formula in H4 : H4公式:

=SUMPRODUCT(MMULT(--(A2:A7=G1:J1),ROW(1:4)/ROW(1:4))*(B1:E1=G4)*B2:E7)

To be complete, there also would be an approach using SUMIF inside SUMPRODUCT which would be the better approach in my opinion. 为了完整SUMPRODUCT ,我认为还有一种在SUMPRODUCT中使用SUMIF的方法,这是更好的方法。

So the Formula in H4 would be: 因此, H4的公式为:

=SUMPRODUCT(SUMIF(A2:A7,G1:J1,INDEX(B2:E7,0,MATCH(G5,B1:E1,0))))

Your formula would be: 您的公式将是:

=SUMPRODUCT(SUMIF(A2:A462,AC1:BD1,INDEX(B2:AA462,0,MATCH("June",B1:AA1,0))))

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

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