11

如何获得基于某个范围的出现次数

  1. 一个正则表达式

  2. 2+条件;假设包含“是”和/或“否”的单元格

我目前所拥有的:

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

我尝试使用COUNTIF(B5:O5; {"*yes*", "*no*"})or COUNTIF(B5:O5; "(*yes*)|(*no*)"),但它们都不起作用。

或者,我如何使用正则表达式计算包含某些域名(yahoo.com、hotmail.com 和 gmail.com)的单元格?例如:

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

4 回答 4

15

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 of cells with "yes" or "no". It will double count a cell with "yesno" or "noyes" since it matches both expressions. You could try to take out the doubles with

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

But that will still get you in trouble with a string like noyesno.

However there is a rather clever trick in Google Docs that may just be a hint of the solution you are looking for:

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

The QUERY function is like a mini database thing. In this case it looks at the table in range A1:A9, and selects only elements in column A where the corresponding element in column A matches (in the preg regex sense of the word) the expression that follows - in this case, "anything followed by yes followed by anything, or anything followed by no followed by anything". In a simple example I made, this counts a yesnoyes only once - making it exactly what you were asking for (I think...)

Right now your range B5:O5 is several columns wide, and only one row high; that makes it hard to use the QUERY trick. Something rather less elegant (but that works regardless of the shape of the range) is this:

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

The sum of the isnumber functions acts as an element-wise OR - unfortunately, the regular OR function doesn't seem to work on individual elements of an array. As before, this finds cells that contain either "yes" or "no", and counts the ones that have either of these strings contained within.

于 2013-08-20T22:49:54.873 回答
2

这深受弗洛里斯回答的启发。尤其是看评论。如果您TRANSPOSE要比较的项目行,也QUERY适用于水平数据:

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

据我所知,Col1是“特殊”且区分大小写的!

于 2019-04-04T01:00:35.690 回答
0

NGix 发现的最简单的方法是为这些目的创建自定义函数。他们添加了 2 个对他们非常有用的东西,并希望它也能帮助别人:

/**
 * 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;
}

/**
 * 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;
}

为了使用它们,只需申请countCustomRegExp(A2:G3;"yes.*")countCustomMatchOr(A2:G3;"yes";"no")

于 2019-03-23T23:46:00.820 回答
-1

我遇到了同样的问题,如果您不想使用VBA,请尝试将 a 添加SUM到您的公式中: =SUM(COUNTIF(B5:O5; {"*yes*", "*no*"}))

于 2018-03-27T23:10:53.963 回答