Forum

Count Color Backgro...
 
Notifications
Clear all

Count Color Background

4 Posts
3 Users
0 Reactions
82 Views
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

I am trying to write a macro to count cells based on color.

Can you help me?

 
Posted : 21/05/2019 2:18 pm
(@mynda)
Posts: 4762
Member Admin
 

Hi,

This tutorial covers counting coloured cells: https://www.myonlinetraininghub.com/count-sum-and-average-colored-cells

Mynda

 
Posted : 21/05/2019 6:04 pm
(@mymalone)
Posts: 103
Estimable Member
Topic starter
 

So, do I have to sum the columns before I can actually count the cells based on the tutorial illustration.  I'm missing a step.  

 
Posted : 22/05/2019 12:15 am
(@debaser)
Posts: 838
Member Moderator
 

No, but you've commented out the code in your workbook and merged two lines together. It should look like this:

Function CountCcolor(range_data As Range, criteria As Range) As Long

Application.Volatile True
Dim datax As Range
Dim xcolor As Longxcolor = criteria.Interior.ColorIndex
For Each datax In range_data
If datax.Interior.ColorIndex = xcolor Then
CountCcolor = CountCcolor + 1
End If
Next datax
End Function

with no apostrophes at the start of each line. Note that you need the function to be volatile to have any chance of it calculating correctly, but note also that changing a cell colour does not trigger a recalculation, so this approach is, in my opinion, fundamentally flawed anyway.

 
Posted : 22/05/2019 4:52 am
Share: