Count the number of events based on two conditions or Regexp

How can I get the number of occurrences for a certain range based on

  1. Regular expression

  2. 2+ conditions; let's say cells that contain yes and / or no

What I have at the moment:

COUNTIF(B5:O5; "*yes*") 

I tried to use COUNTIF(B5:O5; {"*yes*", "*no*"}) or COUNTIF(B5:O5; "(*yes*)|(*no*)") , but none of them have worked.

Or how to count cells that contain some domain names - yahoo.com, hotmail.com and gmail.com - using regexp? eg:

 (\W|^)[\w.+\-]{0,25}@(yahoo|hotmail|gmail)\.com(\W|$) 
+8
source share
4 answers

The most pedestrian solution to your problem (tested in Excel and Google Docs) is to simply add the result of several countif formulas:

 =COUNTIF(B5:O5, "*yes*") + COUNTIF(B5:O5, "*no*") 

This expression will count the total number of cells with yes or no. It will double count the cell with "yesno" or "noyes", since it matches both expressions. You can try to pull the doubles with

 =COUNTIF(B5:O5, "*yes*") + COUNTIF(B5:O5, "*no*") - COUNTIF(B5:O5, "*no*yes*") - COUNTIF(B5:O5, "*yes*no*") 

But it will still cause you problems with a string like noyesno .

However, Google Docs has a pretty tricky trick that might just be a hint of the solution you're looking for:

 =COUNTA(QUERY(A1:A9, "select A where A matches '(.*yes.*)|(.*no.*)'")) 

The QUERY function is similar to the mini-database thing. In this case, he looks at the table in the range A1:A9 and selects only the elements in column A, where the corresponding element in the column A matches (in the meaning of the word preg regex ) is the next expression - in this case, "everything follows yes , after followed by something, or anything followed by no , followed by something. " In the simple example that I did, it is considered yesnoyes only once, which does exactly what you are asking for (I think ...)

Now your range of B5:O5 has a width of several columns and only one row; which makes it harder to use the QUERY trick. Something more or less elegant (but it works regardless of the shape of the range):

 =countif(arrayformula(isnumber(find("yes",A1:A9))+isnumber(find("no",A1:A9))),">0") 

The sum of the isnumber functions acts as an OR element - unfortunately, the regular OR function does not work for individual elements of the array. As before, it finds cells that contain yes or no, and counts those that contain one of these lines contained inside.

+12
source

This is pretty much inspired by Floris' answer . See comments in particular. If you TRANSPOSE series of elements for comparison, QUERY works fine for horizontal data too:

 =COUNTA(QUERY(TRANSPOSE(B5:O5), "select * where Col1 matches '.*(yes|no).*'")) 

As far as I can tell, Col1 β€œspecial” and case sensitive!

+1
source

I had the same problem, if you do not want to use VBA , try adding SUM to your formula: =SUM(COUNTIF(B5:O5; {"*yes*", "*no*"}))

0
source

The easiest way NGix has found is to create a custom function for these purposes. They added 2 that work great for them, and hope this helps someone too:

 /** * Count if cell value matches any condition ( supports regexp also ) */ function countCustomMatchOr(data){ var count = 0; for(var i=0; i < data.length; i++){ for(var j=0; j<data[i].length; j++){ var cell = data[i][j]; for(var k=1;k<arguments.length;k++){ if(typeof arguments[k] == "number"){ if(arguments[k] == cell) count++; } else if(cell.toString().match(arguments[k])) count++; } } } return count; } 

And

 /** * Counts value in data range if matches regular expression */ function countCustomRegExp(data, reg, flag){ var rows = data.length, count = 0, re = flag?new RegExp(reg, flag):new RegExp(reg); for( var i = 0; i < rows; i++){ for( var j = 0; j < data[i].length; j++){ if( data[i][j] != "" && data[i][j].toString().match(re) ){ count++; } } } return count; } 

To use them, just use countCustomRegExp(A2:G3;"yes.*") Or countCustomMatchOr(A2:G3;"yes";"no")

0
source

Source: https://habr.com/ru/post/952153/


All Articles