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

From Apache OpenOffice Wiki
Jump to: navigation, search
(Syntax:)
Line 5: Line 5:
  
 
=== Syntax: ===
 
=== Syntax: ===
<tt>'''COUNTIF(test_range, condition)'''</tt>
+
<tt>'''COUNTIF(test_range; condition)'''</tt>
  
 
: <tt>'''test_range'''</tt> is the range to be tested.
 
: <tt>'''test_range'''</tt> is the range to be tested.
Line 39: Line 39:
 
=== Example: ===
 
=== Example: ===
  
<tt>'''COUNTIF(C2:C8, ">=20")'''</tt>
+
<tt>'''COUNTIF(C2:C8; ">=20")'''</tt>
 
:returns the number of cells in <tt>'''C2:C8'''</tt> whose contents are numerically greater than or equal to <tt>'''20'''</tt>.
 
:returns the number of cells in <tt>'''C2:C8'''</tt> whose contents are numerically greater than or equal to <tt>'''20'''</tt>.
  
<tt>'''COUNTIF(C2:C8, F1)'''</tt>
+
<tt>'''COUNTIF(C2:C8; F1)'''</tt>
 
:where <tt>'''F1'''</tt> contains the text <tt>'''>=20'''</tt>, returns the same number.
 
:where <tt>'''F1'''</tt> contains the text <tt>'''>=20'''</tt>, returns the same number.
  
<tt>'''COUNTIF(C2:C8, "&lt;"&F2)'''</tt>
+
<tt>'''COUNTIF(C2:C8; "&lt;"&F2)'''</tt>
 
:where <tt>'''F2'''</tt> contains <tt>'''20'''</tt> returns the number of cells in <tt>'''C2:C8'''</tt> whose contents are numerically less than <tt>'''20'''</tt>. (<i>Advanced topic:</i> this works because the & operator converts the content of <tt>'''F2'''</tt> to text, and concatenates it with "<"; <tt>'''COUNTIF'''</tt> then converts it back to a number).
 
:where <tt>'''F2'''</tt> contains <tt>'''20'''</tt> returns the number of cells in <tt>'''C2:C8'''</tt> whose contents are numerically less than <tt>'''20'''</tt>. (<i>Advanced topic:</i> this works because the & operator converts the content of <tt>'''F2'''</tt> to text, and concatenates it with "<"; <tt>'''COUNTIF'''</tt> then converts it back to a number).
  
<tt>'''COUNTIF(A2:A8, ">=P")'''</tt>
+
<tt>'''COUNTIF(A2:A8; ">=P")'''</tt>
 
:returns the number of cells in <tt>'''A2:A8'''</tt> whose contents begin with the letter <tt>'''P'''</tt> or later in the alphabet.
 
:returns the number of cells in <tt>'''A2:A8'''</tt> whose contents begin with the letter <tt>'''P'''</tt> or later in the alphabet.
  
<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.
  

Revision as of 12:09, 23 July 2009


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 Tools menu – Options - OpenOffice.org 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:

SUMPRODUCT(B2:B8="Red").

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

Template:Documentation/SeeAlso

Personal tools