COUNTIFS 函数

目录
索引 🔎︎

# COUNTIFS

返回多个范围中满足条件的单元格的计数.

This function is available since LibreOffice 4.0.

#### 语法

COUNTIFS(Range; Criterion[; Range2; Criterion2][; ... ; [Range127; Criterion127]])

Range1 – required argument. It is a range of cells, a name of a named range, or a label of a column or a row, to which the corresponding criterion is to be applied.

Criterion: A criterion is a single cell Reference, Number or Text. It is used in comparisons with cell contents.

A reference to an empty cell is interpreted as the numeric value 0.

A matching expression can be:

- A Number or Logical value. A matching cell content equals the Number or Logical value.

- A value beginning with a comparator (<, <=, =, >, >=, <>).

  For =, if the value is empty it matches empty cells.

  For <>, if the value is empty it matches non-empty cells.

  For <>, if the value is not empty it matches any cell content except the value, including empty cells.

  Note: "=0" does not match empty cells.

  For = and <>, if the value is not empty and can not be interpreted as a Number type or one of its subtypes and the property Search criteria = and <> must apply to whole cells is checked, comparison is against the entire cell contents, if unchecked, comparison is against any subpart of the field that matches the criteria. For = and <>, if the value is not empty and can not be interpreted as a Number type or one of its subtypes applies.

- Other Text value. If the property Search criteria = and <> must apply to whole cells is true, the comparison is against the entire cell contents, if false, comparison is against any subpart of the field that matches the criteria. The expression can contain text, numbers, regular expressions or wildcards (if enabled in calculation options).

范围2 – 可选。「范围2」和以下所有内容的含义与「区域1」相同。

Criterion2 – Optional. Criterion2 and all the following mean the same as Criterion.

The search supports wildcards or regular expressions. With regular expressions enabled, you can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must either precede every character with a "\" character, or enclose the text into \Q...\E. You can switch the automatic evaluation of wildcards or regular expression on and off in LibreOffice - PreferencesTools - Options - LibreOffice Calc - Calculate.

When using functions where one or more arguments are search criteria strings that represents a regular expression, the first attempt is to convert the string criteria to numbers. For example, ".0" will convert to 0.0 and so on. If successful, the match will not be a regular expression match but a numeric match. However, when switching to a locale where the decimal separator is not the dot makes the regular expression conversion work. To force the evaluation of the regular expression instead of a numeric expression, use some expression that can not be misread as numeric, such as ".[0]" or ".\0" or "(?i).0".

Range, Range2, ... and Criterion, Criterion2, ... must have the same size, otherwise the function returns err:502 - Invalid argument.

条件之间的逻辑关系可以定义为逻辑「与」(联接)。换句话说, 当且仅当所有给定条件均满足的情况下, 给定「Func_Range」函数范围的相应单元格中的值才会纳入计算。

该函数最多可以有 255 个参数, 这意味着您可以为它们指定 127 组条件范围和条件。

如果单元格内容为 TRUE，则视为 1, 如果内容为 FALSE – 则视为 0 (零)。

此功能是「用于 Office 应用程序的开放文档格式 (OpenDocument)」标准 1.2 版本的一部分。(ISO/IEC 26300:2-2015)

#### 示例

考虑下表

| A | B | C
1 | 产品名称 | 销售 | 收入
2 | 铅笔 | 20 | 65
3 | 钢笔 | 35 | 85
4 | 笔记本 | 20 | 190
5 | 图书 | 17 | 180
6 | 笔袋 | not | not

在下面的所有示例中, 计算范围都包含行 #6, 该行将被忽略, 因为它包含文本。

#### 简单用法

「=AVERAGE(A1:A50)」

统计范围 B2:B6 中数值大于或等于 20 的行数。返回 3, 因为第五行和第六行不符合条件。

「=AVERAGE(A1:A50)」

统计同时满足 C2:C6 范围内数值大于 70 且 B2:B6 范围内数值大于或等于 20 的行数。返回 2, 因为第二行、第五行和第六行不满足至少一个条件。

#### 使用正则表达式和嵌套函数

For these examples to work as described, make sure that Enable regular expressions in formulas is selected in LibreOffice - PreferencesTools - Options - LibreOffice Calc - Calculate.

「=COUNTIFS(B2:B6;"[:alpha:]*")」

统计 B2:B6 范围内只包含字母符号的行数。返回 1, 因为只有第六行满足条件。

「=COUNTIFS(B2:B6;">"&MIN(B2:B6);B2:B6;"<"&MAX(B2:B6))」

计算 B2:B6 范围的行数, 排除此范围内最小值和最大值所在的行。返回 2, 因为第三行、第五行和第六行不满足至少一个条件。

「=COUNTIFS(A2:A6;"pen.*";B2:B6;"<"&MAX(B2:B6))」

计算对应 A2:A6 区域以「pen」开头的所有单元格且对应 B2:B6 区域除最大值以外的所有单元格的行数。返回 1, 因为只有第二行满足所有条件。

#### 引用单元格作为条件

如果您需要轻松地更改一个条件, 可以考虑在单独的单元格中指定条件, 并在 COUNTIFS 函数的条件中使用对该单元格的引用。例如, 可以按如下方式重写上述函数:

「=COUNTIFS(A2:A6;E2&".*";B2:B6;"<"&MAX(B2:B6))」

如果 E2 = pen, 则该函数返回 1, 因为指向单元格的链接将替换为其内容, 与上述函数行为类似。

相关主题

COUNT, COUNTIF, COUNTBLANK, COUNTA

COUNTIFS, MINIFS, MAXIFS, AVERAGEIFS, SUMIFS

正则表达式列表