Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Counting Colors from Conditional Formats

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

EYVIND    28 Oct 2015, 18:43
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
Rick Rothstein    24 Oct 2015, 11:43
@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.
Eyvind    21 Oct 2015, 17: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!
Roy Taylor    15 Oct 2015, 15:01
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.
Rick Rothstein    10 Oct 2015, 11:15
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).
Robert chang    10 Oct 2015, 09:43
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.
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.