To add the SumByColor Function to your workbook.
Copy this text between the ................................
Function SumByColor(InRange As Range, WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Double
'
' This function return the SUM of the values of cells in
' InRange with a background color, or if OfText is True a
' font color, equal to WhatColorIndex.
'
Dim Rng As Range
Dim OK As Boolean
Application.Volatile True
For Each Rng In InRange.Cells
If OfText = True Then
OK = (Rng.Font.ColorIndex = WhatColorIndex)
Else
OK = (Rng.Interior.ColorIndex = WhatColorIndex)
End If
If OK And IsNumeric(Rng.Value) Then
SumByColor = SumByColor + Rng.Value
End If
Next Rng
End Function
........................................................
Now, with your workbook open hit Alt + F11 to open the Visual Basic Editor.
CTRL + r to open project explorer.
Right-click on your workbook/project and Insert>Module.
Paste the text above into that module.
File>Save then hit Alt + q to return to Excel.
You can call this function from a worksheet cell with a formula like
=SUMBYCOLOR(A1:A10,3,FALSE) where 3 is the colorindex number(red in this
case)
If you don't care about font color just use =SumByColor(A1:A10,3)
To get a list of Excel's default colorindex numbers see David McRitchie's site.
http://www.mvps.org/dmcritchie/excel/colors.htm
Gord Dibben MS Excel MVP