View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.misc
Kirk Bubul[_2_] Kirk Bubul[_2_] is offline
external usenet poster
 
Posts: 9
Default Add numbers if cell fill is a certain color; How To?

On Fri, 22 Feb 2013 11:39:36 -0800, Gord Dibben
wrote:

You want to keep track of the sum of cells that are scattered
throughout a worksheet.

Easist way to do this is include the entire range of cells on the
sheet and pick out just those of a particular color.

1. Open your workbook.
2. Hit Alt + F11 to open Visual Basic Editor
3. Left-click on your workbook name to select it
4. On upper menu hit InsertModule
5. Copy this code into that module

Function SumByColor(InRange As Range, WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Double
'You can call this function from a worksheet cell with a formula like
'=SUMBYCOLOR(A1:A10,3,FALSE)

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


6. Alt + Q to return to your Excel window.................save the
workbook.

7. Color the cells in column G to be summed yellow or a color of your
choice, as long as you know the color index number............CI for
yellow is 6

8. In any empty cell enter =SumByColor(G1:G1000,6) Make the
range large enough to cover future yellow cells.

This sums only the yellow cells.


Hope this helps. Gord

It helps tremendously. Thanks you very much. I'll try to
implement it over the weekend.