
Tips.Net > ExcelTips Home > Macros > VBA Examples > Counting Cells with Text Colors
Summary: Excel allows you to specify text colors on a character-by-character basis in your cells, if desired. If you want to count how many cells contain text of a specific color, the user-defined function in this tip will come in very handy. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Michala has a worksheet of survey responses that includes different text in different colors. For instance, if survey response is "I dislike dogs," the word "dogs" may be colored red, and the rest of the text is black. A response might have multiple colors in it, for instance in the response "I like dogs and cats," the word "dogs" may be in red and the word "cats" in blue. Michala needs a way to highlight a range of cells and count how many cells contain text of a specific color, such as red or blue.
This is best done by developing a user-defined function that can do the counting for you. The following example steps through a range of cells and counts for whatever color index value you specify.
Function CountColorIndex(rng As Range, iColor As Integer)
Dim v As Variant
Dim rCell As Range
Dim str As String
Dim sChar As String
Dim x As Integer
Dim iCount As Integer
iCount = 0
For Each rCell In rng
v = rCell.Font.ColorIndex
If IsNull(v) Then
For x = 1 To Len(rCell.Value)
If rCell.Characters(x, 1).Font.ColorIndex _
= iColor Then
iCount = iCount + 1
Exit For
End If
Next
ElseIf v = iColor Then
iCount = iCount + 1
End If
Next
CountColorIndex = iCount
End Function
The function first looks at the font color of the cell as a whole. If the cell color is Null, that means that the color of individual characters has been changed and so the function starts looking through each character. If it finds the matching color, the count (iCount) is incremented and the function stops looking through each character.
If the cell color is not Null, then the function determines if the font color of the cell as a whole matches the desired color. If it does, then the count is incremented.
This process is repeated for each cell in the specified range, and the function then returns the value of the count. You use the function in the following manner:
=CountColorIndex(B7:D42,3)
This formula checks the range B7:D42 to see if there are instances of the color red. The count is then returned by the formula.
It is worth mentioning that the function relies on color index values. The normal, default value for red is 3 and the value for blue is 5, but these values can be modified by using Tools | Options | Color tab. For the function to return the desired results, you'll need to modify the color index value, specified in the second parameter of the formula, so that it represents the color indexes used in your particular workbook.
Tip #2901 applies to Microsoft Excel versions: 97 2000 2002 2003
Got the Time? Understanding the ins and outs of working with times and dates can be confusing. Remove the confusion--ExcelTips: Times and Dates is an invaluable resource for learning how best to work with times and dates.
Check out ExcelTips: Times and Dates today!
No, not that type of date. If you need to do any types of work with calendar dates, Excel has the tools you need. Learn how to use those tools the easy way. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site