VBA defined function problem sumbycolor

B

Brian Matlack

Hi!
I recently got this bit of code from the forum here and it works great
as long as I format the cells myself but there are two problems
1. It does not work if I use conditional formatting.
2. I have to hit F9 to get it to recalculate the formula if I format
another cell in the range.
Can I get this to work on conditional formatting and auto update?

<Start code>
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

Thanks for any help!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top