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

AutoText Limits

Are there limits to AutoText entries in Word? If you are having problems saving entries, it may not be due to limits imposed ...

Discover More

Condensing and Expanding Headings

When working in Outline view, you'll undoubtedly have the need to expand or condense information under your headings. It is ...

Discover More

Updating an Entire TOC from a Macro

The TOC (Table of Contents) is generated by a field. This field may be updated in a macro using a single command line.

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)

More than Three Conditional Formats

Conditional formatting is a great feature for making the data in your worksheets more understandable and usable. What if you ...

Discover More

Removing Conditional Formats, but Not the Effects

Conditional formatting is very powerful, but at some point you may want to make the formatting "unconditional." In other ...

Discover More

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
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}] 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 five minus 4?

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.


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.