I'm using an updated version of a UDF I found years ago on Ozgrid's website and I've seen many versions of this over the years and through many searches. The UDF allows you to sum or count cells where the background color matched a reference cell's color. A few years ago I updated the code to use .Color instead of .ColorIndex so that it would work with the newer Excel color palettes.
What I would like to do now is have it sum or count if the cell's color matches the colors in a range of cells. Like a 'colored cell subtotal'. I adjusted to UDF to look at cells that did not match a certain cell's background color and I had it point to a cell with no background color and it worked, but unfortunately I also used a gray color in a blank column as a visual separator, so it was counting that column as well. Is it possible to loop through a range of cells and have them each tested against a separate range of cells?
Code:
Function ColorFunction2(rColor As Range, rRange As Range, Optional SUM As Boolean) Dim rCell As Range
Dim lcol As Long
Dim vResult
lcol = rColor.Interior.Color
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.Color = lcol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.Color = lcol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction2 = vResult
End Function
What I would like to do now is have it sum or count if the cell's color matches the colors in a range of cells. Like a 'colored cell subtotal'. I adjusted to UDF to look at cells that did not match a certain cell's background color and I had it point to a cell with no background color and it worked, but unfortunately I also used a gray color in a blank column as a visual separator, so it was counting that column as well. Is it possible to loop through a range of cells and have them each tested against a separate range of cells?