Counting cells by conditional formatting color

tbilderbac

New Member
Joined
Apr 11, 2022
Messages
28
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a spreadsheet that I am inputting golf scores in and for certain colors I am wanting to auto count that color. I got some code that should count the number of red cells [Conditional formatting] in the row. The formula is =CountByColor(G18:X18,E2) but it is not counting up the red cells? I have the target cell conditionally formatted to the color red so it should be working?

1649681555964.png


Public Function CountByColor(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = TargetCell.DisplayFormat.Interior.Color
For Each C In CellRange
If C.DisplayFormat.Color = TargetColor Then Count = Count + 1

Next C
CountByColor = Count
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You missed a property in the test
Rich (BB code):
If C.DisplayFormat.Interior.Color = TargetColor Then Count = Count + 1

Although, I am doubtful that this UDF could be used in a worksheet formula. Refering to the .DisplayFormat in a UDF will return the correct value when called from VBA, but gives a #VALUE error when called by a worksheet formula.
It might be best to test for the condition that causes the CF color rather than for the color itself.
 
Upvote 0
1649684305120.png


1649684346290.png


I am trying to count the RED cells and as you can see I have changed the Set rng = Sheet1.Range ("G18:X18} and the color to the RED (255,0,0) and changed the display to AD18? it is not showing? What am I doing?
 
Upvote 0
You could do it like
VBA Code:
Public Function CountByColor(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = Evaluate("cfcolour(" & TargetCell.Address & ")")
For Each C In CellRange
If Evaluate("Cfcolour(" & C.Address & ")") = TargetColor Then Count = Count + 1

Next C
CountByColor = Count
End Function
Function CFColour(Cl As Range) As Double
   CFColour = Cl.DisplayFormat.Interior.Color
End Function
 
Upvote 0
You could do it like
VBA Code:
Public Function CountByColor(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = Evaluate("cfcolour(" & TargetCell.Address & ")")
For Each C In CellRange
If Evaluate("Cfcolour(" & C.Address & ")") = TargetColor Then Count = Count + 1

Next C
CountByColor = Count
End Function
Function CFColour(Cl As Range) As Double
   CFColour = Cl.DisplayFormat.Interior.Color
End Function
So I am unclear with this? If I put this in a module and then run it it should pull up the number of reds in a row? Where do I put in the color and the range of cells? Sorry not real versed in coding?
 
Upvote 0
You use it exactly the same as the code you initially posted.
 
Upvote 0
Just replace the code you posted, with the code I posted.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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