COUNTIF function

This article describes the formula syntax and usage of the COUNTIF function (function: A prewritten formula that takes a value or values, performs an operation, and returns a value or values. Use functions to simplify and shorten formulas on a worksheet, especially those that perform lengthy or complex calculations.) in Microsoft Office Excel.

Description

The COUNTIF function counts the number of cells within a range that meet a single criterion that you specify. For example, you can count all the cells that start with a certain letter, or you can count all the cells that contain a number that is larger or smaller than a number you specify. For example, suppose you have a worksheet that contains a list of tasks in column A, and the first name of the person assigned to each task in column B. You can use the COUNTIF function to count how many times a person's name appears in column B and, in that way, determine how many tasks are assigned to that person. For example:

=COUNTIF(B2:B25,"Nancy")

 Note   To count cells based on multiple criteria, see COUNTIFS function.

Syntax

COUNTIF(range, criteria)

The COUNTIF function syntax has the following arguments (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.):

  • range    Required. One or more cells to count, including numbers or names, arrays, or references that contain numbers. Blank and text values are ignored.
  • criteria    Required. A number, expression, cell reference, or text string that defines which cells will be counted. For example, criteria can be expressed as 32, ">32", B4, "apples", or "32".

 Notes 

  • You can use the wildcard characters — the question mark (?) and the asterisk (*) — in criteria. A question mark matches any single character, and an asterisk matches any sequence of characters. If you want to find an actual question mark or asterisk, type a tilde (~) before the character.
  • Criteria are case insensitive; for example, the string "apples" and the string "APPLES" will match the same cells.

Example 1: Common COUNTIF formulas

The example may be easier to understand if you copy it to a blank worksheet.

ShowHow do I copy an example?

  1. Select the example in this article. If you are copying the example in Excel Web App, copy and paste one cell at a time.Important Do not select the row or column headers.

Selecting an example from Help

Selecting an example from Help

  1. Press CTRL+C.
  2. Create a blank workbook or worksheet.
  3. In the worksheet, select cell A1, and press CTRL+V. If you are working in Excel Web App, repeat copying and pasting for each cell in the example.

Important For the example to work properly, you must paste it into cell A1 of the worksheet.

  1. To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Formulas tab, in the Formula Auditing group, click the Show Formulas button.

After you copy the example to a blank worksheet, you can adapt it to suit your needs.


 
1
2
3
4
5
6

7

8

9


10

11



12
A B C
Data Data
apples 32
oranges 54
peaches 75
apples 86
Formula Description Result
=COUNTIF(A2:A5,"apples") Number of cells with apples in cells A2 through A5. 2
=COUNTIF(A2:A5,A4) Number of cells with peaches in cells A2 through A5. 1
=COUNTIF(A2:A5,A3)+COUNTIF(A2:A5,A2) Number of cells with oranges and apples in cells A2 through A5. 3
=COUNTIF(B2:B5,">55") Number of cells with a value greater than 55 in cells B2 through B5. 2
=COUNTIF(B2:B5,"<>"&B4) Number of cells with a value not equal to 75 in cells B2 through B5. 3
=COUNTIF(B2:B5,">=32")-COUNTIF(B2:B5,">85") Number of cells with a value greater than or equal to 32 and less than or equal to 85 in cells B2 through B5. 3

Example 2: COUNTIF formulas using wildcard characters and handling blank values

The example may be easier to understand if you copy it to a blank worksheet.

ShowHow do I copy an example?

  1. Select the example in this article. If you are copying the example in Excel Web App, copy and paste one cell at a time.Important Do not select the row or column headers.

Selecting an example from Help

Selecting an example from Help

  1. Press CTRL+C.
  2. Create a blank workbook or worksheet.
  3. In the worksheet, select cell A1, and press CTRL+V. If you are working in Excel Web App, repeat copying and pasting for each cell in the example.

Important For the example to work properly, you must paste it into cell A1 of the worksheet.

  1. To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Formulas tab, in the Formula Auditing group, click the Show Formulas button.

After you copy the example to a blank worksheet, you can adapt it to suit your needs.


 
1
2
3
4
5
6
7
8



9





10



11


12




13




14
A B C
Data Data
apples Yes
   
oranges NO
peaches No
   
apples yes
Formula Description Result
=COUNTIF(A2:A7,"*es") Number of cells ending with the letters "es" in cells A2 through A7. 4
=COUNTIF(A2:A7,"?????es") Number of cells ending with the letters "les" and having exactly 7 letters in cells A2 through A7. 2
=COUNTIF(A2:A7,"*") Number of cells containing any text in cells A2 through A7. 4
=COUNTIF(A2:A7,"<>"&"*") Number of cells not containing text in cells A2 through A7. 2
=COUNTIF(B2:B7,"No") / ROWS(B2:B7) The average number of No votes (including blank cells) in cells B2 through B7. 0.333333333
=COUNTIF(B2:B7,"Yes") / (ROWS(B2:B7) -COUNTIF(B2:B7, "<>"&"*")) The average number of Yes votes (excluding blank cells) in cells B2 through B7. 0.5

 Note    To view a number as a percentage, select the cell and then, on the Sheet tab, in the Number group, click Percentage Style Button image.

 
 
Applies to:
Excel 2007