How to make GetColorCount (custom function) work with Conditional Formatting?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
919
Office Version
  1. 365
  2. 2019
Many years ago, I found this incredibly helpful piece of code that I have used hundreds of times since:

VBA Code:
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
  If rCell.Interior.ColorIndex = CountColorValue Then
    TotalCount = TotalCount + 1
  End If
Next rCell
GetColorCount = TotalCount
End Function

I use it with a formula like this:

Excel Formula:
=@GetColorCount($AF2:$AG2,$AH$1)

Where AH1 is a fill color I've defined, then it looks in AF2:AG2 for matching fill colors, and gives me a count of the matches.

However, It seems it does not work with conditional formatting. Is there a solution to this?

Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Is AH1 manually filled, or is that also from CF?
 
Upvote 0
Is AH1 manually filled, or is that also from CF?

AH1 is a manual Fill Color. I thought I might have seen where you were going with this, so I tried to make AH1 colored via conditional formatting too. However, then it doesn't seem to recognize that the AH1 is filled at all, so it just thinks AH1 has no fill, and counts all the cells in the range (which, incidentally, I changed to $D2:$AG2, so the result is 30).

I also verified with the hex code that I'm using the exact same color in conditional formatting as in AH1, which is FF0000.
 
Upvote 0
Ok, how about
VBA Code:
Function GetColorCount(CountRange As Range, CountColor As Range)
   Dim CountColorValue As Long
   Dim TotalCount As Long
   Dim rcell As Range
   
   CountColorValue = CountColor.Interior.ColorIndex
   For Each rcell In CountRange
     If Evaluate("getcfcolour(" & rcell.Address(, , , True) & ")") = CountColorValue Then
       TotalCount = TotalCount + 1
     End If
   Next rcell
   GetColorCount = TotalCount
End Function
Private Function GetCFColour(Cl As Range) As Long
   GetCFColour = Cl.DisplayFormat.Interior.ColorIndex
End Function
 
Upvote 1
Solution
That worked, thank you! And it looks like you made it work with fill or conditional formatting, so my other instances of using it with fill in the same workbook are not impacted. Thanks!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top