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

ID: Q26698


The information in this article applies to:


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


Keywords          : 
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 

Last Reviewed: March 11, 1999