View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ivano Ivano is offline
external usenet poster
 
Posts: 28
Default count if font and background color condition is true

Hi,
I want to count the number of cells that have the font color in -4105
(autocolor) and a background color of -4142 (no fill). I can't figure out
how to make the code work to count when both conditions are met.

I have found the following code:

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