Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Counting Cells with Text Colors

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 the user, and they may vary based on the version of Excel you are using. 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2901) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Related Tips:

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Nicola    01 Nov 2016, 11:02
Hi,

Is this possible without using the colour index but using a cell with text of that colour in it?

Many thanks,

Nikki
Poolson    05 Jan 2015, 15:20
Hi!
I followed this tip, but it does not work for me. I only get a message that the formula is wrong. It marks that the string H3,3) in my example is wrong. I got cells from C5 to H5. This is my cells.

127 685 482 354 750 128 where 685 and 354 is red. And my formula: =Countcolorindex(C5:H5,3)

I have done my VBA!
I have Excel 2013.
What can be wrong?

Thanks a lot!
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.