Count Cells by Colour

Peter Davison

Active Member
Joined
Jun 4, 2020
Messages
439
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
 
Both seem okay.
I changed to the red colour from the pallet but still didn't work and the trust centre below seems okay

1685373264037.png
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The code works quite happily.
Try putting this in the same module & then from Excel press Alt F8 & select the macro & click run. Do you get the message box
VBA Code:
Sub check()
   MsgBox "hi"
End Sub
 
Upvote 0
In that case the function should work, although it will not update automatically.
 
Upvote 0
Did you colour the cells before or after entering the formula?
 
Upvote 0
I think the problem is the cells it is trying to count are formulas returning a number.
I just put some colours in cells with no formulas and it counted them okay.
Is there something could be added to the code to get round the formularised cells?
 
Upvote 0
I just tested it by entering the colour into a formula cell and it works.
Do you think it won't work because of conditional formatting
 
Upvote 0

Forum statistics

Threads
1,215,123
Messages
6,123,183
Members
449,090
Latest member
bes000

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