Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Counting Cells with Specific Characters.
by Allen Wyatt
(last updated October 14, 2019)
Let's say that you have a worksheet that contains all the people who have ever worked in your department. Each name is prefaced by a single character that indicates the status of the person. For instance, if Fred Davis were retired, his name might show up as "RFred Davis". With quite a lot of these names in the worksheet, you may need a way to count those people with a specific status character.
The easiest way to accomplish this is to use the COUNTIF function. If, for instance, the status character is the letter R (for "retired"), and your range of names is in cells A5:A52, then you could use the following to determine which cells begin with the letter R:
The formula works because the comparison value is R*, which means "the letter R followed by any other characters." Excel dutifully returns the count. To search for a different status character, simply replace R with the desired status character.
Obviously, if the asterisk has a special meaning in this usage, you can't search directly for an asterisk. Actually, there are three characters you cannot search for directly: the asterisk (*), the question mark (?) and the tilde (~). If you want to search for any of these characters, you must precede the character with the tilde. Thus, if you wanted to determine a count of names that had a question mark as a status code, you could use the following:
An alternative to using COUNTIF is to create an array formula that is applied to every cell in the range. The following will do the trick very nicely:
This must, of course, be entered as an array formula. This means that instead of pressing Enter at the end of the formula, you would press Shift+Ctrl+Enter. The formula checks the left-most character of a cell, returning the value TRUE if it is R or FALSE if it is not. The multiplication is done to convert the TRUE/FALSE value to a number, either 1 for TRUE or 0 for FALSE. The SUM function returns the sum, or count, of all the cells that meet the criteria.
One final note: The formulas provided in this tip are a way to deal with the data as originally presented at the first of the tip. If you have any control at all over your data, you really should consider removing the "R" (or any other leading status character) from the names. Those sort of characters really deserve their own column, instead of complicating the name field. Use the Text to Columns tool to separate out the "R" and any other leading characters, and then you can more easily work with them in your formulas.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9871) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Counting Cells with Specific Characters.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Sometimes it can be confusing to figure out the source of an error that is displayed in your worksheet. Excel provides a ...Discover More
Need to count the number of cells in a range that are not blank? You can use the COUNTA function of a more complex ...Discover More
Got some formulas you slaved over and want to use in lots of workbooks? This tip presents some helpful ideas on how you ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.