VBA - Count cells by color

sofiachr

Board Regular
Joined
Jan 15, 2013
Messages
98
Hi!

I have the below VBA code and use the formula =CountCellsByColor($A$2:$A$9;D1) where I want to sum my colors. A2:A9 is where my list is in my test document and D1 being my referens for what color to look for. Problem is I get the right count first time but when I change one cells color in the range A2:A9 the sum doesn´t change. If I however copy another cell with the color I want and paste in the cell I´m changing then the sum will change. I don´t know how to fix this. Not everyone using the file knows this is required, they need to be able to just format the cell as normal and the sum must follow.

VBA Code:
Function GetCellColor(xlRange As Range)
    Dim indRow, indColumn As Long
    Dim arResults()
 
    Application.Volatile
 
    If xlRange Is Nothing Then
        Set xlRange = Application.ThisCell
    End If
 
    If xlRange.Count > 1 Then
      ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count)
       For indRow = 1 To xlRange.Rows.Count
         For indColumn = 1 To xlRange.Columns.Count
           arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color
         Next
       Next
     GetCellColor = arResults
    Else
     GetCellColor = xlRange.Interior.Color
    End If
End Function
 
 
Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long
 
    Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Interior.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Interior.Color Then
            cntRes = cntRes + 1
        End If
    Next cellCurrent
 
    CountCellsByColor = cntRes
End Function
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You could put this in the relevant sheet module
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Calculate
End Sub
although all formulae on that sheet will recalculate whenever you select a different cell.
Also once you have changed the colour of D1 you will need to select another cell.
 
Upvote 0
You could put this in the relevant sheet module
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Calculate
End Sub
although all formulae on that sheet will recalculate whenever you select a different cell.
Also once you have changed the colour of D1 you will need to select another cell.
Hi, no change unfortunately.
/Sofia
 
Upvote 0
You could put this in the relevant sheet module
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Calculate
End Sub
although all formulae on that sheet will recalculate whenever you select a different cell.
Also once you have changed the colour of D1 you will need to select another cell.
Sorry, to fast. It work perfect, thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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