Excel - counting cells with a specific colour (including conditional formatting)

MZ1

New Member
Joined
Jan 12, 2022
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Hi Everyone,
this is my first post on this forum. I'm writing cause I can't find a satisfying solution to my problem.
I need to count the number of cells with specific Interior Colour. For now, to do so I was using Visual Basic function as below:

VBA Code:
Function CountColor(CellsRange As Range, ReferenceCell As Range)
 Dim indRefColor As Long
 Dim Format As Range

Application.Volatile
CountColor= 0
indRefColor = ReferenceCell.Interior.Color
For Each cell In CellsRange

    If indRefColor = cell.Interior.Color Then

     CountColor= CountColor+ 1

    End If

Next
End Function

To sum up:
I want to modify the above function so that it counts for cells with the same interior colour as the selected one (and includes cells with conditional formatting).

Thank you for your time and help.
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi & welcome to MrExcel.
How about
VBA Code:
Function CountColor(CellsRange As Range, ReferenceCell As Range)
   Dim indRefColor As Long
   Dim Format As Range
  
   Application.Volatile
   CountColor = 0
   indRefColor = ReferenceCell.Interior.Color
   For Each cell In CellsRange
      If Evaluate("CFColour(" & cell.Address & ")") = indRefColor Then
         CountColor = CountColor + 1
      End If
   Next
End Function
Function CFColour(Cl As Range) As Double
   CFColour = Cl.DisplayFormat.Interior.Color
End Function
 
Last edited:
Upvote 0
If Evaluate("CFColour(" & Cl.Address & ")") = indRefColor Then
cell.Address?

I was also wondering if the ReferenceCell might also be coloured by Conditional Formatting?
 
Upvote 0
cell.Address?
Oops, well spotted, I've changed code in post#2
I was also wondering if the ReferenceCell might also be coloured by Conditional Formatting?
Good point, this will handle that situation.
Excel Formula:
Function CountColor(CellsRange As Range, ReferenceCell As Range)
   Dim indRefColor As Long
   Dim Format As Range
   
   Application.Volatile
   CountColor = 0
   indRefColor = Evaluate("cfcolour(" & ReferenceCell.Address & ")")
   For Each cell In CellsRange
      If Evaluate("CFColour(" & cell.Address & ")") = indRefColor Then
         CountColor = CountColor + 1
      End If
   Next
End Function
Function CFColour(Cl As Range) As Double
   CFColour = Cl.DisplayFormat.Interior.Color
End Function
 
Upvote 0
Solution
Thank you very much. It works perfectly.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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