Tried to convert a working subroutine into a function but now it doesn't work. I'm not good with Range variables so I'm sure it's user error. Intent is to have function count the cells in a given range that have the same color as a single specified cell.
Cell Formulas | ||
---|---|---|
Range | Formula | |
B1 | =CountColor(A1,B4:D5) | |
B2 | =CountColor(A2,B4:D5) |
Code:
Function CountColor(rColor As Range, rRange As Range) As Long
'rColor is cell with background color of interest
'rRange is range of cells to consider
Dim rMyRng As Range
Dim iBackColor As Long
On Error Resume Next
iBackColor = 0
For Each rMyRng In rRange
If rMyRng.DisplayFormat.Interior.Color = rColor.DisplayFormat.Interior.Color Then
iBackColor = iBackColor + 1
End If
Next
CountColor = iBackColor
End Function