Counting Colors from Conditional Formats

by Allen Wyatt
(last updated October 10, 2015)

6

Ronald has a worksheet that utilizes conditional formatting. The conditions result in the cells being different colors. He wants to count the number of cells that are red in the worksheet. He knows how to create a macro that will examine the cell color and do a count if a cell is formatted directly as red, but the macro won't work with cells that are conditionally formatted. Ronald wants to know if there is a way to count these conditionally red cells, as well.

You cannot directly check in a macro what the color of a cell is based on a conditional format. There are ways you can work around this with a macro, but it is not for the faint-of-heart. The following page on Chip Pearson's site demonstrates the difficulty in determining conditional colors:

http://www.cpearson.com/excel/CFColors.htm

Given the difficulty of the task, it may just be easier to recreate the conditions within the macro, and then see which cells meet these conditions. The result is that you count cells matching conditions rather than count cells that are colored red as a result of those conditions. This should yield the same count of cells, but is much easier to handle programmatically.

Of course, the only caveat to this solution is that you will need to keep the conditions in the macro and the conditions in the conditional formats in sync with each other. If you change one and fail to change the other, then you won't get the desired results.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2873) 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

Setting Text Attributes

Want to change the attributes of your text (or what Excel refers to as font styles)? Here's how to do it.

Discover More

Renaming a Macro

Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.

Discover More

Setting Default Print Margins

Don't like the print margins that Excel uses by default? You can change the default by changing the workbook on which Excel ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

MORE EXCELTIPS (MENU)

Conditional Formats that Distinguish Blanks and Zeroes

Conditional formatting is a great tool. You may need to use this tool to tell the difference between cells that are empty or ...

Discover More

Shading a Cell Until Something is Entered

Conditional formatting provides the opportunity to get very creative with your formatting. One such creative urge can be ...

Discover More

Changing Coordinate Colors

Tired of the default colors that Excel uses to display the row and column coordinates? You can modify the colors, but only if ...

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 for this tip:

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. 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 8 - 1?

2015-10-28 18:43:00

EYVIND

Hello Rick , thank for the answer.

Well Iam not an expert programmer, I would like with your code can check any range of columns with conditional formatting(in this case painting red as your example) and return the value in other cell, maybe as a function or procedure, because I need identify how many columns are in red.

I hope I explained.
Thank you


2015-10-24 11:43:58

Rick Rothstein

@Eyvind,

Remove this line of code...

Set Rng = Application.InputBox("Select a range", Type:=8)

and change the subroutine header for the first macro I posted (do similarly for the second one) to this...

Sub CountAllRedCells(Rng As Range)

But after you do that, it is no longer a macro, rather, it is a VBA subroutine that would need to be called by a normal Excel macro where you would pass whatever range you want to examine as an argument to the subroutine.


2015-10-21 17:31:31

Eyvind

Hello, that example is very good.

I would like to know how to modify the macro and past the range like a parameters without use the inputbox.

Thanks!


2015-10-15 15:01:02

Roy Taylor

I have been scouring the web for a way of finding the colour set by conditional formatting but to no avail.
Thank you for this it is excellent, and so simple.


2015-10-10 11:15:42

Rick Rothstein

If you are using XL2010 or above, the Range object has a property that has not been advertised all that much which can see CF properties of a cell along with non-CF properties... the DisplayFormat property. It is an object and using its Interior.Color property allows you to see the color being displayed in the cell no matter how that color got into the cell. Here is a macro that will count all red cells in the selected range...

Sub CountAllRedCells()
Dim ColorCount As Long, Cell As Range, Rng As Range
On Error GoTo NoRangeSelected
Set Rng = Application.InputBox("Select a range", Type:=8)
On Error GoTo 0
If TypeOf Rng Is Range Then
For Each Cell In Rng
If Cell.DisplayFormat.Interior.Color = vbRed Then
ColorCount = ColorCount + 1
End If
Next
End If
MsgBox "Number of red cells in Range " & _
Rng.Address(0, 0) & " is " & ColorCount
Exit Sub
NoRangeSelected:
End Sub

If you only wanted to count the red cells that came from Conditional Formatting, then use this macro instead...

Sub CountConditionallyFormattedRedCells()
Dim ColorCount As Long, Cell As Range, Rng As Range
On Error GoTo NoRangeSelected
Set Rng = Application.InputBox("Select a range", Type:=8)
On Error GoTo 0
If TypeOf Rng Is Range Then
For Each Cell In Rng
If Cell.DisplayFormat.Interior.Color = vbRed Then
ColorCount = ColorCount - (Cell.Interior.Color <> vbRed)
End If
Next
End If
MsgBox "Number of red cells in Range " & _
Rng.Address(0, 0) & " is " & ColorCount
Exit Sub
NoRangeSelected:
End Sub

Note that this last macro is not foolproof in that theoretically a cell could be naturally red and Conditionally Formatted as red, both at the same time... that cell will not be counted as being Conditionally Formatted red by the macro. However, I don't mind this flaw as having such a Conditional Format makes no sense (you wouldn't conditionally color a red cell red, you simply wouldn't conditionally color it at all and let its natural color show through).


2015-10-10 09:43:23

Robert chang

Here is an approach I used to count conditional formatted cells.
Count the number of rows that are being displayed from a filtered column with the function subtotal.
Use a macro to iterate over each color you want to count using the filter's subtotal and then you would be able to extract the conditional formatting count of rows for specific colors.


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.

Links and Sharing
Share