Forum

Cell counting by co...
 
Notifications
Clear all

Cell counting by color

6 Posts
4 Users
0 Reactions
142 Views
(@usb)
Posts: 244
Honorable Member
Topic starter
 

Hi,
In the attached file there is a VBA code that gives me a function that counts by color.
The problem is that it doesn't work with conditional formatting
In the table the yellow cells were created from conditional design
And in the sample cell on this side is normal coloring
what can we do??
How do you count the yellow cells in data based on conditional formatting?
Thank you!
Leah

 
Posted : 29/12/2023 6:53 am
Riny van Eekelen
(@riny)
Posts: 1194
Member Moderator
 

You can't count conditionally formatted colors. In stead, you should use COUNTIF and use the condition for setting the  format as the criteria. In your example file you could use:

=COUNTIF(INDEX(A2:H22,,MATCH(J4,A1:H1,0)),">300")

 
Posted : 29/12/2023 8:53 am
(@keebellah)
Posts: 373
Reputable Member
 

Hi Riny, I disagree, it is possible

 

Function countRowsWithConditionalColor(data_range As Range, cell_color As Range) As Long
Dim cntRes As Long, cel As Range
For Each cel In data_range
On Error Resume Next
If cel.DisplayFormat.Interior.Color = cell_color.Interior.Color Then
cntRes = cntRes + 1
End If
Next cel
Err.Clear
On Error GoTo 0
countRowsWithConditionalColor = cntRes
End Function

Attached a copy of the (working) file

 
Posted : 30/12/2023 4:55 am
(@debaser)
Posts: 837
Member Moderator
 

Hi Hans,

It doesn't work properly though. DisplayFormat cannot be reliably used in a UDF - try updating some of the values so they go yellow and your function won't update.

You can work around it using Evaluate but it's not a good idea in my opinion.

 
Posted : 30/12/2023 5:30 am
(@keebellah)
Posts: 373
Reputable Member
 

It's as reliable as the color you choose/pick. That's where the problem lies

 
Posted : 02/01/2024 4:28 pm
(@debaser)
Posts: 837
Member Moderator
 

I disagree. It will not work properly in a UDF - Microsoft even states that in the documentation.

 
Posted : 02/01/2024 7:19 pm
Share: