Question : Sum cells based on their background color with Worksheet_Change Event

Hello:
I am wondering if it is possible to add up all cells within a range that have a given background color.  

As I chg cells in the range to a different color or switch colors around it re-calculates each colors new value.

I added a sample worksheet that may show better what I am trying to do.
Please advise and thanks. -R-

Answer : Sum cells based on their background color with Worksheet_Change Event

Sorry, last time :)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Public Function SumColor(rngData As Range, intColor As Integer) As Double
Dim rngCell As Range
Dim dblSum As Double
' Interior.ColorIndex = 3    'red
' Interior.ColorIndex = 5    'blue
' Interior.ColorIndex = 4    'green
' Interior.ColorIndex = 6    'yellow
' Interior.ColorIndex = 44   'gold
    Application.Volatile
    For Each rngCell In rngData
        If rngCell.Interior.ColorIndex = intColor Then
            On Error Resume Next
            If Err.Number = 0 Then
                If Application.WorksheetFunction.Type(rngCell.Value) = 1 Then
                    dblSum = dblSum + rngCell.Value
                End If
            Else
                Err.Clear
            End If
        End If
    Next
    SumColor = dblSum
End Function
Random Solutions  
 
programming4us programming4us