Written by Allen Wyatt (last updated May 6, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Conditional formatting is very powerful, but at some point you may want to make the formatting "unconditional." In other ...
Discover MoreIf you need to shade alternating rows in a data table, you'll want to examine how you can accomplish the task with ...
Discover MoreTired of the default colors that Excel uses to display the row and column coordinates? You can modify the colors, but ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments