Difference between revisions of "Documentation/How Tos/Calc: COUNTIF function"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (See also:)
m
 
(13 intermediate revisions by 9 users not shown)
Line 1: Line 1:
__NOTOC__
+
{{DISPLAYTITLE:COUNTIF function}}
 +
{{Documentation/CalcFunc MathematicalTOC
 +
|ShowPrevNext=block
 +
|PrevPage=Documentation/How_Tos/Calc:_COUNTBLANK_function
 +
|NextPage=Documentation/How_Tos/Calc:_DELTA_function
 +
}}__NOTOC__
  
 
== COUNTIF  ==
 
== COUNTIF  ==
Line 21: Line 26:
 
: In this case <tt>'''COUNTIF'''</tt> compares those cells in <tt>'''test_range'''</tt> with the remainder of the text string (interpreted as a number if possible or text otherwise).
 
: In this case <tt>'''COUNTIF'''</tt> compares those cells in <tt>'''test_range'''</tt> with the remainder of the text string (interpreted as a number if possible or text otherwise).
  
: For example the condition “<tt>'''>4.5'''</tt>” tests if the content of each cell is greater than the number 4.5, and the condition “<tt>'''<dog'''</tt>” tests if the content of each cell would come alphabetically before the text <tt>'''dog'''</tt>.
+
: For example, the condition “<tt>'''>4.5'''</tt>” tests if the content of each cell is greater than the number 4.5, and the condition “<tt>'''<dog'''</tt>” tests if the content of each cell would come alphabetically before the text <tt>'''dog'''</tt>.
  
  
: It can be very important to check the settings on the '''Tools menu Options - OpenOffice.org Calc - Calculate''' dialog:
+
: It can be very important to check the settings on the menu {{menu|Tools|Options|OpenOffice Calc|Calculate}} dialog:
  
 
:: If the checkbox is ticked for ''<nowiki>search criteria = and <> must apply to whole cells</nowiki>'', then the condition “<tt>'''red'''</tt>” will match only <tt>'''red'''</tt><nowiki>; if unticked it will match </nowiki><tt>'''red'''</tt>, <tt>'''Fred'''</tt>, <tt>'''red herring'''</tt>.
 
:: If the checkbox is ticked for ''<nowiki>search criteria = and <> must apply to whole cells</nowiki>'', then the condition “<tt>'''red'''</tt>” will match only <tt>'''red'''</tt><nowiki>; if unticked it will match </nowiki><tt>'''red'''</tt>, <tt>'''Fred'''</tt>, <tt>'''red herring'''</tt>.
Line 30: Line 35:
 
:: If the checkbox is ticked for ''Enable regular expressions in formulas'', the condition will match using [[Documentation/How_Tos/Regular Expressions in Calc|regular expressions]] - so for example "<tt>'''r.d'''</tt>" will match <tt>'''red'''</tt>, <tt>'''rod'''</tt>, <tt>'''rid'''</tt>, and "<tt>'''red.*'''</tt>" will match <tt>'''red'''</tt>, <tt>'''redraw'''</tt>, <tt>'''redden'''</tt>.
 
:: If the checkbox is ticked for ''Enable regular expressions in formulas'', the condition will match using [[Documentation/How_Tos/Regular Expressions in Calc|regular expressions]] - so for example "<tt>'''r.d'''</tt>" will match <tt>'''red'''</tt>, <tt>'''rod'''</tt>, <tt>'''rid'''</tt>, and "<tt>'''red.*'''</tt>" will match <tt>'''red'''</tt>, <tt>'''redraw'''</tt>, <tt>'''redden'''</tt>.
  
:: The checkbox for ''Case sensitive'' has no effect (no attention is paid to case). See the examples for how to achieve a case sensitive match.
+
:: The checkbox for ''Case sensitive'' has no effect (no attention is paid to case). See the examples for how to achieve a case-sensitive match.
  
  
 
: Blank (empty) cells in <tt>'''test_range'''</tt> are ignored (they never satisfy the condition).
 
: Blank (empty) cells in <tt>'''test_range'''</tt> are ignored (they never satisfy the condition).
  
: <tt>'''condition'''</tt> can only specify one single condition. See '''[[Documentation/How_Tos/Conditional Counting and Summation|Conditional Counting and Summation]]''' for ways to specify multiple conditions.
+
: <tt>'''condition'''</tt> can only specify one single condition. See [[Documentation/How_Tos/Conditional Counting and Summation|Conditional Counting and Summation]] for ways to specify multiple conditions.
  
 
=== Example: ===
 
=== Example: ===
Line 53: Line 58:
 
<tt>'''COUNTIF(B2:B8; "red")'''</tt>
 
<tt>'''COUNTIF(B2:B8; "red")'''</tt>
 
:returns the number of cells in <tt>'''B2:B8'''</tt> containing <tt>'''red'''</tt>, but this number may depend on the option settings discussed above.
 
:returns the number of cells in <tt>'''B2:B8'''</tt> containing <tt>'''red'''</tt>, but this number may depend on the option settings discussed above.
 +
 +
  
 
<i>Advanced topic:</i>
 
<i>Advanced topic:</i>
 +
 +
<tt>'''COUNTIF(B2:B8; ".+")'''</tt>
 +
:returns the number of cells in <tt>'''B2:B8'''</tt> containing one or more character, e.g. <tt>'''not blank'''</tt>, using the syntax of [[Documentation/How_Tos/Regular_Expressions_in_Writer|regular expressions]].
  
 
<tt>'''SUMPRODUCT(B2:B8="Red")'''</tt>.  
 
<tt>'''SUMPRODUCT(B2:B8="Red")'''</tt>.  
 
:returns the number of cells in <tt>'''B2:B8'''</tt> matching <tt>'''Red'''</tt>, with case sensitivity. See [[Documentation/How_Tos/Conditional Counting and Summation|Conditional Counting and Summation]] for details.
 
:returns the number of cells in <tt>'''B2:B8'''</tt> matching <tt>'''Red'''</tt>, with case sensitivity. See [[Documentation/How_Tos/Conditional Counting and Summation|Conditional Counting and Summation]] for details.
  
=== See also: ===
+
{{SeeAlso|EN|
'''[[Documentation/How_Tos/Calc: SUMIF function|SUMIF]]''',
+
* [[Documentation/How_Tos/Calc: SUMIF function|SUMIF]]
'''[[Documentation/How_Tos/Calc: COUNT function|COUNT]]''',
+
* [[Documentation/How_Tos/Calc: COUNT function|COUNT]]
'''[[Documentation/How_Tos/Calc: COUNTA function|COUNTA]]''',
+
* [[Documentation/How_Tos/Calc: COUNTA function|COUNTA]]
'''[[Documentation/How_Tos/Calc: COUNTBLANK function|COUNTBLANK]]''',
+
* [[Documentation/How_Tos/Calc: COUNTBLANK function|COUNTBLANK]]
'''[[Documentation/How_Tos/Calc: DCOUNT function|DCOUNT]]''',
+
* [[Documentation/How_Tos/Calc: DCOUNT function|DCOUNT]]
'''[[Documentation/How_Tos/Calc: DCOUNTA function|DCOUNTA]]'''
+
* [[Documentation/How_Tos/Calc: DCOUNTA function|DCOUNTA]]
  
'''[[Documentation/How_Tos/Calc: SUMPRODUCT function|SUMPRODUCT]]''',
+
* [[Documentation/How_Tos/Calc: SUMPRODUCT function|SUMPRODUCT]]
'''[[Documentation/How_Tos/Calc: SUBTOTAL function|SUBTOTAL]]'''
+
* [[Documentation/How_Tos/Calc: SUBTOTAL function|SUBTOTAL]]
  
'''[[Documentation/How_Tos/Conditional Counting and Summation|Conditional Counting and Summation]]'''
+
* [[Documentation/How_Tos/Conditional Counting and Summation|Conditional Counting and Summation]]
  
'''[[Documentation/How_Tos/Regular Expressions in Calc|Regular Expressions in Calc]]'''
+
* [[Documentation/How_Tos/Regular Expressions in Calc|Regular Expressions in Calc]]
  
[[Documentation/How_Tos/Calc: Functions listed alphabetically|'''Functions listed alphabetically''']],
+
* [[Documentation/How_Tos/Calc: Functions listed alphabetically|Functions listed alphabetically]]
[[Documentation/How_Tos/Calc: Functions listed by category|'''Functions listed by category''']]
+
* [[Documentation/How_Tos/Calc: Functions listed by category|Functions listed by category]]}}
 +
[[Category: Documentation/Reference/Calc/Mathematical functions]]

Latest revision as of 15:15, 31 January 2024

COUNTIF

Counts the number of cells in a range that meet a specified condition.

Syntax:

COUNTIF(test_range; condition)

test_range is the range to be tested.
condition may be:
a number, such as 34.5
an expression, such as 2/3 or SQRT(B5)
a text string
COUNTIF counts those cells in test_range that are equal to condition, unless condition is a text string that starts with a comparator:
>, <, >=, <=, =, <>
In this case COUNTIF compares those cells in test_range with the remainder of the text string (interpreted as a number if possible or text otherwise).
For example, the condition “>4.5” tests if the content of each cell is greater than the number 4.5, and the condition “<dog” tests if the content of each cell would come alphabetically before the text dog.


It can be very important to check the settings on the menu Tools → Options → OpenOffice Calc → Calculate dialog:
If the checkbox is ticked for search criteria = and <> must apply to whole cells, then the condition “red” will match only red; if unticked it will match red, Fred, red herring.
If the checkbox is ticked for Enable regular expressions in formulas, the condition will match using regular expressions - so for example "r.d" will match red, rod, rid, and "red.*" will match red, redraw, redden.
The checkbox for Case sensitive has no effect (no attention is paid to case). See the examples for how to achieve a case-sensitive match.


Blank (empty) cells in test_range are ignored (they never satisfy the condition).
condition can only specify one single condition. See Conditional Counting and Summation for ways to specify multiple conditions.

Example:

COUNTIF(C2:C8; ">=20")

returns the number of cells in C2:C8 whose contents are numerically greater than or equal to 20.

COUNTIF(C2:C8; F1)

where F1 contains the text >=20, returns the same number.

COUNTIF(C2:C8; "<"&F2)

where F2 contains 20 returns the number of cells in C2:C8 whose contents are numerically less than 20. (Advanced topic: this works because the & operator converts the content of F2 to text, and concatenates it with "<"; COUNTIF then converts it back to a number).

COUNTIF(A2:A8; ">=P")

returns the number of cells in A2:A8 whose contents begin with the letter P or later in the alphabet.

COUNTIF(B2:B8; "red")

returns the number of cells in B2:B8 containing red, but this number may depend on the option settings discussed above.


Advanced topic:

COUNTIF(B2:B8; ".+")

returns the number of cells in B2:B8 containing one or more character, e.g. not blank, using the syntax of regular expressions.

SUMPRODUCT(B2:B8="Red").

returns the number of cells in B2:B8 matching Red, with case sensitivity. See Conditional Counting and Summation for details.



See Also
Retrieved from "https://wiki.openoffice.org/w/index.php?title=Documentation/How_Tos/Calc:_COUNTIF_function&oldid=259887"
Views
Personal tools
Navigation
Tools
In other languages