vba code to count cell color

superfb

Active Member
Joined
Oct 5, 2011
Messages
255
Office Version
  1. 2007
Platform
  1. Windows
Hi

I am manually adding a specific color to a cell (Yellow) as a result i would like to count the number of cells that have a color.. i came across this code

Code:
Function CountColor(Rng As Range, RngColor As Range) As Integer
    Dim Cll As Range
    Dim Clr As Long
    Clr = RngColor.Range("N1").Interior.Color
    For Each Cll In Rng
        If Cll.Interior.Color = Clr Then
            CountColor = CountColor + 1
        End If
    Next Cll
End Function
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You didn't ask a question.
I'm guessing you need to make this change :
Code:
Clr = RngColor.Interior.Color
 
Upvote 0
You didn't ask a question.
I'm guessing you need to make this change :
Code:
Clr = RngColor.Interior.Color

Apologies - i wanted to count the number of cells colored and the code above wasnt working correctly.....it is now although the number doesnt instantly change when i take the color off....
 
Upvote 0
Not exactly what you want but will trigger recalculation of the function when selection is changed :
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Calculate
End Sub
 
Upvote 0
Not exactly what you want but will trigger recalculation of the function when selection is changed :
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Calculate
End Sub


Thank you for this.

i keep getting value error


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)Me.Calculate
End Sub


Function CountColor(Rng As Range, RngColor As Range) As Integer
    Dim Cll As Range
    Dim Clr As Long
    
    Clr = RngColor.Interior.Color
    For Each Cll In Rng
        If Cll.Interior.Color = Clr Then
            CountColor = CountColor + 1
        End If
    Next Cll


End Function
 
Upvote 0
Do you mean the function is returning #VALUE !

If so, have you entered in the formula the cell ref containing the color to be counted?
 
Upvote 0
Do you mean the function is returning #VALUE !

If so, have you entered in the formula the cell ref containing the color to be counted?

that is correct, the first part of the formula reference the column c, and i want the formula in o2? maybe my interpretation is wrong?
 
Upvote 0
If, for examole, the range to count is C1:C10 and the cell with the reference color is N1, put in O2 (or in any other cell) :

=CountColor(C1:C10,N1)
 
Upvote 0
Ahhh i see but colm c is the column i want to count how many cells have color........
 
Upvote 0
It is counting col C, N1 can be any cell that has the same colour fill as the cells you want to count.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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