[英]Formula to count cells in a range that match any value in cells in another range
I am looking for a formula that will count the number of cells in a range (say A1:A5) whose values match any of the values of another range (say B1:B3).我正在寻找一个公式来计算一个范围(比如 A1:A5)中其值与另一个范围(比如 B1:B3)的任何值匹配的单元格的数量。
Edit: I am also looking for a formula that doesn't break randomly.编辑:我也在寻找一个不会随机中断的公式。 See bottom.*
见底部。*
If A1:A5 is:如果 A1:A5 是:
1
2
3
4
5
and B1:B3 is: B1:B3 是:
3
4
8
the answer should be:答案应该是:
2
since A3 and A4 match something in B1:B3.因为 A3 和 A4 匹配 B1:B3 中的内容。
I expected the method shown here to work: https://exceljet.net/formula/count-total-matches-in-two-ranges我希望此处显示的方法有效: https://exceljet.net/formula/count-total-matches-in-two-ranges
=SUMPRODUCT(COUNTIF(range1,range2))
...but it doesn't. ...但事实并非如此。 Maybe that's because the range sizes are not the same?
也许那是因为范围大小不一样?
=COUNTIF(range1,range2)
also doesn't work.也不起作用。
(My second range of cells is also on another sheet, but I can't imagine that would matter.) (我的第二个单元格范围也在另一张纸上,但我无法想象这会很重要。)
Seems like there should be a simple formula for this kind of thing!看来这种东西应该有个简单的公式吧!
*Edit: *编辑:
I tried the formula above a 2nd time, for the trivial example I gave, and this time it worked.我第二次尝试了上面的公式,因为我给出了一个简单的例子,这次成功了。 The formula is unreliable , though.
不过,这个公式是不可靠的。 It can fail for some unknown reason as shown below.
它可能由于某些未知原因而失败,如下所示。 It sometimes fails to read the 2nd COUNTIF range, and from testing today it seems to fail if there is no outer SUMPRODUCT.
它有时无法读取第二个 COUNTIF 范围,从今天的测试来看,如果没有外部 SUMPRODUCT,它似乎会失败。 Why would an outer formula element matter??
为什么外部公式元素很重要?
With just the COUNTIF part...只有 COUNTIF 部分......
...it fails (and returns 0 instead of the expected 1). ...失败(并返回 0 而不是预期的 1)。
With the exact same formula fragment (unless my eyes deceive me) wrapped in an outer SUMPRODUCT()...使用完全相同的公式片段(除非我的眼睛欺骗了我)包裹在外部 SUMPRODUCT()...
As Solar Mike mentioned in the comments: Perhaps count matches .正如 Solar Mike 在评论中提到的: Perhaps count matches 。
=COUNT(MATCH($A$1:$A$10,$B$1:$B$10,0))
=SUMPRODUCT(--ISNUMBER(MATCH($A$1:$A$10,$B$1:$B$10,0)))
=SUMPRODUCT(--(COUNTIF($B$1:$B$10,$A$1:$A$10)>0))
The COUNTIF
Trap COUNTIF
陷阱
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.