Counting Colors of Cells

by Allen Wyatt
(last updated November 13, 2015)

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

Drawing a Table

There are several ways you can create tables in a document, but one of the most unique (and perhaps most fun) is to ...

Discover More

Fitting to a Single Page

It can be frustrating when a single-page document actually prints of two pages, depending on the system that is doing the ...

Discover More

Inserting an Image On a Specific Page

Macros are great for processing a document just the way you want. You can even use them to insert graphics, as described ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

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

Generating Unique Sequential Numbers

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

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
Subscribe

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

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}] 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 four more than 0?

2019-03-29 03:25:28

Jan H.

Excellent tip.

How can I use this if I need to SUM the value of yellow coloured cells found, in a range of multicolored cells

2016-12-14 04:10:14

rakesh sharma

I need to say:

If any cell is red, then: 0 without any VBA ...i dont know how can i set formula for this
can anyone tell me how can i put formula for colour

I'm looking to do this. Please can you instruct me on this...

cell color is red than I get returned value is 0
Name: rake.png

2015-05-24 17:30:18

Heather

Hello,

How would the following be written?

If cells are x color on sheet 1 then copy to sheet 2

Thank you!

2014-10-02 17:34:19

Siahaan

check if A1 is filled color (ex.yellow)

regards

2013-03-28 09:20:17

Sorry for the mutliple comments. Looked at Barry's entry and I already have the text "Application.volatile" inside the macro

2013-03-28 09:17:09

To get the results from the macro, Ineed to do an F9 autocalculate. I look in Options and it is set to automatic, so I am assuimng that the there is VBA script in this macro that calls for manual recalculation that is overwriting this. Anyone have an idea how to get it to autocalc.

2013-03-28 09:15:10

I'm an expert with the program, but a novice with VBA and writing macrso.
The Macro works great. I had a little trouble with the color assignment, but that is not the thing that I am writing about.

I need to do an F9 autocalculate. I look in Options and it is set to automatic, so I am assuimng that the there is VBA script in this macro that calls for manual recalculation that is overwriting this. Anyone have an idea how to get it to autocalc.

2012-12-19 13:16:39

Barry Fitzpatrick

The line of code:

Application.volatile

should be added as the second line, to make the function more responsive to re-calculation of the spreadsheet.

The second parameter is the ColorIndex to be matched. This can have a value from 1-56. A full list of the colors can be found at http://www.automateexcel.com/2004/08/18/excel_color_reference_for_colorindex/

2012-12-19 05:27:36

Barry Fitzpatrick

The following function (which should be copied to a VBA Module) acts similar to the usual IF function in Excel, but interrogates the colour of the specified cell. The result can be text or a number.

Function IfColour(MyRange As Range, Colour As Integer, Match As Variant, NoMatch As Variant) As Variant

If MyRange.Interior.ColorIndex = Colour Then
IfColour = Match
Else
IfColour = NoMatch
End If

End Function

To test the colour in cell A1 use the following formula:

=IfColour(A1,6,"Matches", "Doesn't Match")

2012-12-19 04:00:19

Jose Geronez

This is the "if" formula I want to make in excel:

@ if cell A1 color background is "yellow", then "swift", otherwise " ".

Can this be done?

Thank you.

2012-04-12 11:17:36

Mark Coleman

OK. After using the site's search function, it is clear that there is not an easy solution for what I want to do. My apologies for not going there first.

Mark Coleman

2012-04-12 10:54:33

Mark Coleman

I found your article very easy to understand and to use, even though I'm a complete beginner with VBA.

However, when I tried it in a file (Excel 2007) that I use every month, it fails to recognize cells that turned yellow due to a CONDITIONAL FORMATTING event. It only recognizes yellow cells that are made yellow with the FILL command. I already made sure the file was saved in 2007 format (*.xlsm).

Is there a way around this?

If I have to change each cell manually, it defeats the purpose of performing this task in Excel instead of on paper.

Best regards,
Mark Coleman

2012-01-31 10:19:43

Brian

Thank you.. i found meny differnt formats but this is the only one i could get to work for me. i will have to dig in a little more to see just how to change the colors, but all in all it was very helpfull
thanks

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.