XL: How to Count the Occurrences of a Number/Text in a Range

Last reviewed: February 2, 1998
Article ID: Q26698

The information in this article applies to:
  • Microsoft Excel for Windows, versions 4.0, 4.0a, 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for the Macintosh, versions 1.x, 2.2, 3.0, 4.0, 5.0, 5.0a
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

In Microsoft Excel, you can use worksheet functions to count the number of occurrences of a specific number or text string in a range of cells on a worksheet. The "More Information" section of this article contains sample formulas you can use to do this.

MORE INFORMATION

In the cell you want the result to appear in, enter the appropriate formula from the following examples.

To Count the Occurrences of a Number

Use this formula

   =SUM(IF(<range>=<number>,1,0))

where <range> is the range you want to search and <number> is the number you want to find.

To Count the Occurrences of a Text String

Use this formula

   =SUM(IF(<range>="<text>",1,0))

where <range> is the range you want to search and <text> is the text you want to find (the text must be enclosed in parenthesis).

NOTE: The above formulas must be entered as array formulas. To enter a formula as an array formula in Microsoft Excel for Windows, press CTRL+SHIFT+ENTER. In Microsoft Excel for the Macintosh, press COMMAND+ENTER.

To Count the Occurrences of a Text String (Version 5.0 and Later)

Use the COUNTIF() function to count the occurrences of a text string. For example, use the formula

   =COUNTIF(<range>,"<text>")

where <range> is the rang of cells you are evaluating and <text> is the text string you want to count instances of (note that <text> must be enclosed in quotation marks).

NOTE: The above formula must be entered as an array formula. To enter a formula as an array formula in Microsoft Excel for Windows, press CTRL+SHIFT+ENTER. In Microsoft Excel for the Macintosh, press COMMAND+ENTER.

Wildcard characters can be used within the COUNTIF function.

The asterisk character (*) represents more than one character. For example, to count all the cells in the range a1:a10 that contained an "x," we can use the following formula:

   =COUNTIF(a1:a10,"*x*")

The question mark character (?) can also be used to represent one wildcard character. For example, to count all cells in the range whose second character is the letter, such as "ax" or "bx."

   =COUNTIF(a1:a10,"?x*")


Additional query words: howto answer XL98 XL97 XL7 XL5 XL4 XL3
Version : WINDOWS:4.0,4.0a,5.0,5.0c,7.0,97; MACINTOSH:1.x,2.2,3.0,4.0,5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.