Counting Cells with Text Colors

Written by Allen Wyatt (last updated March 5, 2022)
This tip applies to Excel 97, 2000, 2002, and 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 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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Resize Graphics Outside of Excel

Graphics are a common addition to almost any workbook. If you need to change the size of your graphics (which Excel lets ...

Discover More

Summing Only the Largest Portion of a Range

Given a range of cells, you may at some time want to calculate the sum of only the largest values in that range. Here is ...

Discover More

Removing Return Addresses

Word allows you to print return addresses on your envelopes. You may run across a scenario where the return address is ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Controlling Display of Toolbar Buttons

You can assign your macros to a series of custom toolbar buttons, but you may only want those buttons to be visible when ...

Discover More

Running a Macro when a Workbook is Closed

One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...

Discover More

Extracting Proper Words

If you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.