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
 
Glad we could help & thanks for the feedback.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

I tried using this same code but it does not appear to work for me. Apologies as I am new to coding. I have the same set up as tbilderbac where on one worksheet I have users enter values, and there is a conditional formatting set that changes the cell color whenever a criteria is met. I am now trying to consolidate the results on a separate worksheet by counting the number of colored cells.

Hope you can help me on this one.

1666819690026.png


Here's how I tried to run the said function:

1666819890557.png


Do I need to save the Excel file as .xlsm first for it to run? PLease advise.
 
Upvote 0
it worked then stopped working now I am getting the #name? error?
 
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
Hey, I was excited to find this code but it does not seem to count the cells correctly for me. When I try to find the number of gray cells in January, it results in 12 instead of 9 as seen in the screenshot. If not for the first condition marked as "stop if True", the other Sunday and Saturday blocks would be highlighted gray, so I assume this code somehow is checking if a conditional format exist that to make the cell a certain color??? Is there a way of only looking at the end result of the color based on all conditions?

1698262504688.png
 

Attachments

  • 1698262386100.png
    1698262386100.png
    229.5 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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