View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Toppers
 
Posts: n/a
Default Sum Highlighted Cells

This is code from Chip Pearson's site which sums cells with a given backgound
colour
or font.

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

You can call this function from a worksheet cell with a formula like
=SUMBYCOLOR(A1:A10,3,FALSE) <==Background colour 3

=SUMBYCOLOR(A1:A10,3,TRUE) <== Font colour 3

HTH

"Jenny" wrote:

I have highlighted cells within a spreadsheet by changing the box color to
yellow. Is there anyway to easily create a formula to SUM these cells for
me? Thanks