简体   繁体   English

Excel公式 - 彼此2个相同?

[英]Excel formula - 2 countif one in another?

That's my first question here ever, though I am reading questions here since a few year. 这是我在这里的第一个问题,虽然我在这里读了几年的问题。

I am looking for a way to do the following with excel formula count how many are line matching a criteria. 我正在寻找一种方法来执行以下excel公式计数有多少是符合条件的行。 Sounds maybe easy, but so far I didn't manage it, probably because I didn't do it the right way. 听起来可能很容易,但到目前为止我没有管理它,可能是因为我没有以正确的方式做到这一点。

I have a table of this kind (here pets, but also work with any "object" array, like worker and their efficiency) 我有一张这样的桌子(这里有宠物,但也适用于任何“对象”阵列,如工人及其效率)

        01.10.2018          02.10.2018              03.10.2018      
        Menu        Wg  Sz  Menu        Wg  Sz  Menu        Wg  Sz
Lassie  Dry food    23  65  Dry food    22  65  Dry food    23  65
Fusel   Meat fodder 12  49  Dry food    14  49  Fish fodder 13  49
Bobo    Fish fodder 33  86  Meat fodder 32  86  Meat fodder 34  86

I am asking myself the questions like this: How many pets ate Fish fodder? 我问自己这样的问题:有多少宠物吃鱼饲料? How many pets are under 50cm? 有多少只50厘米以下的宠物?

I can do easily this on a row level and then add a sum cell (let's say in A column): 我可以在行级别上轻松执行此操作,然后添加sum单元格(假设在A列中):

COUNTIF(3:3,"Fish fodder")
COUNTIF(4:4,"Fish fodder")
COUNTIF(5:5,"Fish fodder")
COUNTIF(A:A,">0")

But I am looking for a way to do this in a formula for single cell. 但我正在寻找一种方法来实现单细胞的公式。

I was thinking to use the crtl+shif+enter way, but then i also need to do it on each row an extra cell to be able cumulate the results. 我正在考虑使用crtl + shif +输入方式,但是我还需要在每一行上添加一个额外的单元格以便能够累积结果。

I hope someone can help. 我希望有人能帮帮忙。

Thank you. 谢谢。

According the COUNTIF formula you gave, I guess this is something you need. 根据您给出的COUNTIF公式,我想这是您需要的。

在此输入图像描述

B9 =SUMPRODUCT(--(MMULT(--($B$3:$J$5=$A9),TRANSPOSE(COLUMN($B$3:$J$3)))>0))
B10 =SUMPRODUCT(--(MMULT(--($B$3:$J$5=$A10),TRANSPOSE(COLUMN($B$3:$J$3)))>0))
B11 =SUMPRODUCT(--(MMULT(--(($B$3:$J$5<50)*(($B$2:$J$2)="Sz")),TRANSPOSE(COLUMN($B$3:$J$3)))>0))

All formulas here are Array Formula so please press Ctrl + Shift + Enter to complete them. 这里的所有公式都是数组公式,所以请按Ctrl + Shift + Enter完成它们。

The trick is, in matrix [nxm]*[mx 1] = [nx 1] . 诀窍是,矩阵[nxm]*[mx 1] = [nx 1] However in excel, matrix * matrix directly is not a matrix multiplication [#1]. 但是在excel中,矩阵*矩阵不是矩阵乘法[#1]。 Array * array returns an array with a11*b11, a12*b12, a13*b13 and so on. Array *数组返回一个数组,其中包含11 a11*b11, a12*b12, a13*b13 ,依此类推。 We have to use a formula called MMULT for matrix multiplying. 我们必须使用一个名为MMULT的公式进行矩阵乘法。

Therefore we built up a [3 x 9] matrix first, and we compare it with the criteria "Dry food" then. 因此我们首先建立了一个[3 x 9]矩阵,然后将其与"Dry food"标准进行比较。 We get a [3 x 9] matrix full of True or False , so we add double minus sign before the matrix, forcing them become 1 and 0 . 我们得到一个充满TrueFalse[3 x 9]矩阵,因此我们在矩阵之前添加双减号,强制它们变为10

The TRANSPOSE is for generating a [9 x 1] matrix, the value is actually not so important once they are greater than 0. Actually we can use a ROW(1:9) and the effect will be the same. TRANSPOSE用于生成[9 x 1]矩阵,一旦它们大于0,该值实际上并不那么重要。实际上我们可以使用ROW(1:9)并且效果将是相同的。 However not everyone knows how to adjust the reference in ROW() . 然而,不是每个人都知道如何调整ROW()的引用。 A benefit of TRANSPOSE(COLUMN()) is that the reference inside is just the same as the origin data area. TRANSPOSE(COLUMN())好处是内部引用与原始数据区域相同。

After executing MMULT , the result become a [3 x 1] matrix. 执行MMULT ,结果变为[3 x 1]矩阵。 And if it is matched with the criteria, the value is greater than 0, others will be 0. So the next part is checking every elements in side the matrix is >0 or not. 如果它与标准匹配,则值大于0,其他值为0.因此,下一部分是检查矩阵中的每个元素是否>0 And then we add a double minus sign again for converting the boolean to 0 and 1 . 然后我们再次添加一个双减号,将布尔值转换为01 The last part here is simply sum them up by SUMPRODUCT . 这里的最后一部分简单地由SUMPRODUCT总结。

[#1] More about matrix multiplication here: https://en.wikipedia.org/wiki/Matrix_multiplication [#1]有关矩阵乘法的更多信息,请访问: https//en.wikipedia.org/wiki/Matrix_multiplication

You could do this with a simple array formulas, they would need to be configured specific to each question. 您可以使用简单的数组公式执行此操作,它们需要针对每个问题进行特定配置。 Here are some examples: 这里有些例子:

Specific Name, Date, and Food: 具体名称,日期和食物:

在此输入图像描述

Food Type By Date: 食物类型按日期:

在此输入图像描述

These are using array multiplication, basically you end up with an array of 1s and 0s and you just sum them. 这些是使用数组乘法,基本上你最终得到一个1和0的数组,你只需求它们。

These are array formulas and must be confirmed with Ctrl+Shift+Enter 这些是数组公式,必须使用Ctrl+Shift+Enter

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

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