How do I use the SumIF function with a criteria of a cell color?

I

Infernoo1

Hi,
I am trying to use the SumIF function and use a criteria based on the color
of the cells. I see I can sort and filter based on cell color, but cannot
find how to add based on the cell color. Is this available, or do I need to
wait for Microsoft to create a =color() function?
 
J

John C

Are the cells just randomly colored? Or are they conditionally formatted to
be a specific color based on certain conditions. If the latter, then you
could do a SUMIF based on the same conditions.
 
B

Bob Phillips

Microsoft are not going to create a Volour fuynction, so write your own.

Function SumByColour(rng As Range, ci)
Dim cnt As Long
Dim cell As Range

Application.Volatile

For Each cell In rng

If cell.Interior.ColorIndex = ci Then

cnt = cnt + 1
End If
Next cell
SumByColour = cnt
End Function


and in you worksheet

=SumByColour(A1:A10,3)

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
R

ryguy7272

I think this will give you what you need:

Function CountByColor(InRange As Range, _
WhatColorIndex As Integer, _
Optional OfText As Boolean = False) As Long
'
' This function return the number of cells in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
'
Dim rng As Range
Application.Volatile True

For Each rng In InRange.Cells
If OfText = True Then
CountByColor = CountByColor - _
(rng.Font.ColorIndex = WhatColorIndex)
Else
CountByColor = CountByColor - _
(rng.Interior.ColorIndex = WhatColorIndex)
End If
Next rng

End Function


Regards,
Ryan---
 

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