简体   繁体   English

用于计算一个范围内的单元格与另一个范围内的单元格中的任何值匹配的公式

[英]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 部分...... 只有 COUNTIF 部分......

...it fails (and returns 0 instead of the expected 1). ...失败(并返回 0 而不是预期的 1)。 ...失败(并返回 0 而不是预期的 1)。

With the exact same formula fragment (unless my eyes deceive me) wrapped in an outer SUMPRODUCT()...使用完全相同的公式片段(除非我的眼睛欺骗了我)包裹在外部 SUMPRODUCT()... 使用完全相同的公式片段(除非我的眼睛欺骗了我)包裹在外部 SUMPRODUCT()...

...it works. ...有用。 Why the difference??为什么不一样?? ...有用。为什么不一样??

Yes it does matches please refer the image below,是的,它确实匹配请参考下图,

=SUMPRODUCT(COUNTIF($A$1:$A$5,$B$1:$B$3))

or或者

You may try this formula, as well,你也可以试试这个公式,

=SUMPRODUCT(--ISNUMBER(MATCH($A$1:$A$5,$B$1:$B$3,0)))

配方_溶液

Count Matches计数匹配

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.

相关问题 需要一系列单元格的公式 - 根据另一个单元格区域中存在的值来突出显示单元格 - Need formula for a range of cells - to highlight cells based on the presence of a value in another range of cells Excel公式,用于确定范围中的任何单元格是否包含值 - Excel formula to determine if any cells in a range contain a value 是否有一个 Excel 公式来查找/匹配一系列单元格中的单词 - Is there an Excel formula to find/match words in a range of cells 将单元格范围(仅限值而非公式)复制到同一文件中的另一个工作表 - Copy (only value and not formula) range of cells to another sheet in same file 使用Excel条件格式-想要突出显示与另一个单元格区域中的任何单元格匹配的区域中的单元格 - With Excel Conditional Formatting - Want to Highlight Cells in a range that match any from another range of cells 需要帮助,Excel公式,计算与不同范围内容匹配的范围内的单元格数 - Need help, Excel Formula, Count number of cells in a range that match the content of a different range 数组公式进入单元格范围 - Array formula into range of cells "将公式设置为一系列单元格" - Set formula to a range of cells 在一系列细胞中使用公式 - Using a formula in a range of cells 将公式应用于一系列单元格 - Apply formula to a range of cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM