Fail in formula

Marcelino

New Member
Joined
Jun 25, 2010
Messages
13
Hi,

I've search everywhere but i still don't have solve of my problem.

I need a formula, which count cells by colour of text. I found a lot of formulas, but all of them count blank cells too.(there are empty, but formatted)

The formula which I'm using is:

Code:
Function CountColor(InRange As Range, ColorIndex As Long, _
    Optional OfText As Boolean = False) As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' CountColor
' This function counts the cells in InRange whose ColorIndex
' is equal to the ColorIndex parameter. The ColorIndex of the
' Font is tested if OfText is True, or the Interior property
' if OfText is omitted or False. If ColorIndex is not a valid
' ColorIndex (1 -> 56, xlColorIndexNone, xlColorIndexAutomatic)
' 0 is returned. If ColorIndex is 0, then xlColorIndexNone is
' used if OfText is Fasle or xlColorIndexAutomatic if OfText
' is True. This allows the caller to use a value of 0 to indicate
' no color for either the Interior or the Font.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim r As Range
Dim N As Long
Dim CI As Long

If ColorIndex = 0 Then
    If OfText = False Then
        CI = xlColorIndexNone
    Else
        CI = xlColorIndexAutomatic
    End If
Else
    CI = ColorIndex
End If


Application.Volatile True
Select Case ColorIndex
    Case 0, xlColorIndexNone, xlColorIndexAutomatic
        ' OK
    Case Else
        If IsValidColorIndex(ColorIndex) = False Then
            CountColor = 0
            Exit Function
        End If
End Select

For Each r In InRange.Cells
    If OfText = True Then
        If r.Font.ColorIndex = CI Then
            N = N + 1
        End If
    Else
        If r.Interior.ColorIndex = CI Then
            N = N + 1
        End If
    End If
Next r

CountColor = N

End Function</pre>

Maybe i can use formula counta, but i don't know how.

Please help:)
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try

Rich (BB code):
For Each r In InRange.Cells
    If OfText = True Then
        If r.Font.ColorIndex = CI And r.Value <> "" Then
            N = N + 1
        End If
    Else
        If r.Interior.ColorIndex = CI And r.Value <> "" Then
            N = N + 1
        End If
    End If
Next r
 
Upvote 0
Can't you just add a test that the cells aren't empty?

Code:
For Each r In InRange.Cells
    If Len(r.Value) > 0 Then
        If OfText = True Then
            If r.Font.ColorIndex = CI Then
                N = N + 1
            End If
        Else
            If r.Interior.ColorIndex = CI Then
                N = N + 1
            End If
        End If
    End If
Next r
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,356
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top