Function ColorMath(InputRange As Range, ReferenceCell As Range, Optional Action As String = "S")
Application.Volatile
' Action can be S to SUM, A to AVERAGE, or C to COUNT
' If not specified the default Action is SUM
Dim ReferenceColor As Long
Dim CellCount As Long
Dim Result As Variant
Dim cell As Range
Action = UCase(Action)
ReferenceColor = ReferenceCell.Interior.Color
If Action = "S" Or Action = "A" Then
For Each cell In InputRange
If cell.Interior.Color = ReferenceColor Then
Result = WorksheetFunction.Sum(cell, Result)
CellCount = CellCount + 1
End If
Next cell
End If
If Action = "C" Then
For Each cell In InputRange
If cell.Interior.Color = ReferenceColor Then Result = Result + 1
Next cell
End If
If Action = "A" Then
Result = Result / CellCount
End If
ColorMath = Result
End Function