简体   繁体   English

如何在Google表格中设置COUNTIF或COUNTIFS公式以比较列?

[英]How to set up COUNTIF or COUNTIFS formula in Google Sheets to compare columns?

I am trying to compare the numerical data in two columns in Google Sheets (say, Col. A and B) and return a count of all of the times that they vary by say, more than 1 (eg, if A3 = 5 and B3 = 2, this should get counted). 我正在尝试比较Google表格中两列中的数值数据(例如,A和B列),并返回它们相差超过1的所有时间的计数(例如,如果A3 = 5和B3 = 2,则应计算在内)。 The two-column arrays will always be of equal size. 两列数组将始终具有相等的大小。

At first, I thought that either COUNTIF or COUNTIFS would be my go-to tool, but I can't get this to work with either formula. 起初,我以为COUNTIFCOUNTIFS都是我的COUNTIFS工具,但是我无法COUNTIFS与任何一个公式一起使用。 These formulas seem to handle criteria within a cell, but - as far as I can tell - can't handle criteria comparing data within two different (adjacent) cells. 这些公式似乎可以处理单元格中的条件,但是-据我所知-无法处理比较两个不同(相邻)单元格中的数据的条件。

Can someone help me with some super syntax work-around to get COUNTIF / COUNTIFS to work... or is there a more appropriate formula to the job (perhaps involving FILTER )? 有人可以帮我一些超级语法的解决方法,以使COUNTIF / COUNTIFS起作用...还是有一个更合适的公式来完成这项工作(也许涉及FILTER )?

*Quick Edit : I know I could always add an additional column, which would be very simple in this example. *快速编辑 :我知道我总是可以添加一个额外的列,在此示例中这将非常简单。 But my real-world spreadsheets are a lot more complex and are already suffering from column overload. 但是我的实际电子表格要复杂得多,并且已经遭受列超载的困扰。 A lot of other formulas are already set up around existing columns, and I was hoping to discover a more elegant solution that would allow me to come up with the count without having to add a new column for each and every comparison calculation. 在现有列周围已经建立了许多其他公式,我希望发现一种更优雅的解决方案,该解决方案使我能够计算出计数而不必为每个比较计算都添加新列。

Add a third column, containing eg =ABS(SUM(A3-B3)) . 添加第三列,其中包含=ABS(SUM(A3-B3)) (The ABS gives you the positive difference regardless of which value is larger.) (无论哪个值较大, ABS都会给您带来正的差异。)

At the bottom of that column, use COUNTIF like =COUNTIF(C1:C25, ">1") (where C1:C25 is the range of cels containing those positive differences). 在该列的底部,使用COUNTIF,例如=COUNTIF(C1:C25, ">1") (其中C1:C25是包含这些正差的cel的范围)。

=ARRAYFORMULA(IF(LEN(A:A&B:B), IF(A:A-B:B>1, 1, )+IF(B:B-A:A>1, 1, ), ))

0

if you want final sum instead of "per row" count use: 如果您想要最终的总和而不是“每行”计数,请使用:

=SUM(ARRAYFORMULA(IF(LEN(A:A&B:B), IF(A:A-B:B>1, 1, )+IF(B:B-A:A>1, 1, ), )))

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

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