Count Cells by Colour

Peter Davison

Active Member
Joined
Jun 4, 2020
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Is it possible to count cells by colour in Excel?
I read on a website is wasn't possible unless the code below was added.

Do you feel this is possible?
I am on 365
Thanks for your help

Function CountCellsByColor(data_range As Range, cell_color As Range) As Long
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Long
Application.Volatile
cntRes = 0
indRefColor = cell_color.Cells(1, 1).Interior.Color
For Each cellCurrent In data_range
If indRefColor = cellCurrent.Interior.Color
Then
cntRes = cntRes + 1
End If
Next cellCurrent
CountCellsByColor = cntRes
End Function
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If the cells are manually coloured then you will need the macro. A formula cannot "see" formatting
 
Upvote 0
Solution
Yup it looks ok & it needs to go in a standard module (not a sheet module)
 
Upvote 0
I put the code in as per the detail below in a module
I then put the formula in the cell - =countcellsbycolor($I$21:$I$5000,$H$18) - See Pic below but don't get a count.
Any thoughts?

1685371561083.png


1685371601125.png
 
Upvote 0
That looks to be in a sheet module, not a standard module.
 
Upvote 0
I'm a bit vague on VBA could you advise me how to get it into a standard mode.
Thanks
 
Upvote 0
Just copy the code & paste it into module1
 
Upvote 0
Ok, check that macros are enabled & that the cells are exactly the same colour.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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