Counting Colors of Cells

Written by Allen Wyatt (last updated August 10, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003


Besides using values and text in your worksheets, Excel allows you to use colors to either enliven or provide meaning to your data. If you use colors in your worksheets, you may wonder if there is a way to count the number of cells that are formatted with a particular fill color. There is no intrinsic function in Excel to perform such a task, but you can certainly make one with a user-defined function. The following is an example of one that will count the number of cells in a range that are formatted with a yellow fill color:

Function CountYellow(MyRange As Range)
    Dim iCount As Integer
    Application.Volatile
    iCount = 0
    For Each cell In MyRange
        If cell.Interior.ColorIndex = 6 Then
            iCount = iCount + 1
        End If
    Next cell
    CountYellow = iCount
End Function

To use the function, all you need to do is use a formula such as the following in a cell of your worksheet:

=CountYellow(A1:A99)

This example returns the number of cells in the range of A1:A99 that use the yellow fill color.

Notice in the CountYellow function that the cells are examined to see if the ColorIndex property is equal to 6. In other VBA coding you may be used to seeing near-English constants that define colors. In this case, the normal color constants don't work. Instead, the ColorIndex property works based on a set of index values into a particular palette of colors. If you are interested in seeing the various index values used for the different colors, take a look at the VBA online help file for the ColorIndex property.

Once you know how to walk through the cells in a range in this manner, it is easy to perform other types of operations based on the color used to fill cells in the range. For instance, instead of simply counting the number of cells, you could add up the values of the cells in the range, or you could find the average of the values in the range. All you need to do is to make the appropriate changes in the code in the innermost If ... End If structure.

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 (1978) 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

Moving Through a Table in a Macro

Do you need to step through a table, cell by cell, in a macro? It's easy to do using the Move method, as described in ...

Discover More

Putting Cell Contents in Footers

Referencing information between cells in a worksheet is a piece of cake using some elemental formulas. You cannot, ...

Discover More

Changing Shortcut Properties

Want to change how a shortcut behaves when you double-click it? Just display the Properties dialog box for the shortcut ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Determining Combinations to Make a Total

If you have a range of cells that contain values, you may wonder which combinations of those cells should be used to meet ...

Discover More

Sorting by Fill Color

Sorting data in a worksheet is easy, unless you want to sort by the color used to fill a range of cells. There are ways ...

Discover More

Generating Unique Sequential Numbers

Using Excel to generate unique sequential numbers for invoices or company statements can be a challenge. Here's ...

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 1 + 1?

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.