# Count Colored Cells



## rwmill9716 (Jan 2, 2023)

I need a function or macro that reads the color in Cell D2 (gold), then counts the appearance of that color in each row and places that count in column D. I want the count to run between Columns K and A and rows 4 to 500. Thanks for your time.


----------



## FryGirl (Jan 2, 2023)

I'm not entirely sure what columns you are talking about.  It seems K:O

=CountCellsByColor(K4:O4,D2)


```
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
```


----------



## rwmill9716 (Jan 2, 2023)

Thanks, FryGirl,

As you can see, when I use your function, I just get zeros. Perhaps, I'm housing it in the wrong area. If I am, can you tell me where it needs to be hourse?

Thanks again.


----------



## FryGirl (Jan 2, 2023)

Put the code in a normal module


----------



## rwmill9716 (Jan 2, 2023)

The count is 0 in each row of column D, though there are gold cells within the data range.


----------



## FryGirl (Jan 2, 2023)

I'm not sure why.  Here is what I get.


----------



## Fluff (Jan 3, 2023)

How are the cells being coloured? Manually or via conditional formatting?


----------

