Counting Colors of Cells
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
iCount = 0
For Each cell In MyRange
If cell.Interior.ColorIndex = 6 Then
iCount = iCount + 1
CountYellow = iCount
To use the function, all you need to do is use a formula such as the following in a cell of your worksheet:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1978) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Leave your own comment:
Comments for this tip:
Heather 24 May 2015, 17:30
How would the following be written?
If cells are x color on sheet 1 then copy to sheet 2
Siahaan 02 Oct 2014, 17:34
check if A1 is filled color (ex.yellow)
please help me to solve this problem using formula
Steve Nadel 28 Mar 2013, 09:20
Sorry for the mutliple comments. Looked at Barry's entry and I already have the text "Application.volatile" inside the macro
Steve Nadel 28 Mar 2013, 09:17
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.
Steve Nadel 28 Mar 2013, 09:15
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.
Barry Fitzpatrick 19 Dec 2012, 13:16
The line of code:
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/
Barry Fitzpatrick 19 Dec 2012, 05:27
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
IfColour = NoMatch
To test the colour in cell A1 use the following formula:
=IfColour(A1,6,"Matches", "Doesn't Match")
Jose Geronez 19 Dec 2012, 04:00
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?
Mark Coleman 12 Apr 2012, 11:17
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 12 Apr 2012, 10:54
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.
Thanks in advance for your time.
Brian 31 Jan 2012, 10:19
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