CountColor -- Match to Cell Font Color

richard blaine

Board Regular
Joined
May 19, 2005
Messages
67
Trying to count items in a column based on the font color of a cell. For instance, if A1= "Xcolor" font and there are five "Xcolor" font cells B1:B100, then C1=5 (C1 contains the formula). Thanx
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Domski

Well-known Member
Joined
Jan 18, 2005
Messages
7,292
Here's a UDF that will do what you want:

Code:
Function CountFontColour(Range As Range, CCode As Double) As Double
    Dim YourDataRange As Range
    Dim kount As Double
    Dim cell As Range
    Application.Volatile
    Set YourDataRange = Intersect(Range.Parent.UsedRange, Range)
    kount = 0
    For Each cell In YourDataRange
        If cell.Font.ColorIndex = CCode Then kount = kount + 1
    Next cell
    CountFontColour = kount
End Function

Use it like: =CountFontColour(B1:B100,3) to count red coloured writing.

For a list of colour codes run this on a blank woorksheet:

Code:
Sub GetColours()
    Dim x As Integer
 
    Application.ScreenUpdating = False
        For x = 1 To 56
            Cells(x, 1) = x
            Cells(x, 2).Interior.ColorIndex = x
        Next x
    Application.ScreenUpdating = True
End Sub

Hope it helps,

Dom
 
Upvote 0

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Try using this custom formula:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Rich (BB code):
<o:p></o:p>
Rich (BB code):
Rich (BB code):
Function CountColour(iRange As Range, FindColour As Range)<o:p></o:p>
Dim LookupColour As Long, iCount As Long<o:p></o:p>
Dim r As Range<o:p></o:p>
LookupColour = FindColour.Interior.ColorIndex<o:p></o:p>
iCount = 0<o:p></o:p>
For Each r In iRange<o:p></o:p>
    If r.Interior.ColorIndex = LookupColour Then<o:p></o:p>
        iCount = iCount + 1<o:p></o:p>
    End If<o:p></o:p>
Next r<o:p></o:p>
CountColour = iCount<o:p></o:p>
End Function<o:p></o:p>
<o:p></o:p>
<o:p> </o:p>
Then in the cell where you want the result, type:<o:p></o:p>
=CountColour(A2:A50,A1)<o:p></o:p>
<o:p> </o:p>
Where A2:A50 is the range you are checking and A1 is the cell whose colour you are checking for.<o:p></o:p>
<o:p> </o:p>
Hope that helps.<o:p></o:p>
 
Upvote 0

richard blaine

Board Regular
Joined
May 19, 2005
Messages
67
Lewiy's formula is perfect for counting cell (fill) color but I would like the same for counting font color. Is that possible? Thanks
 
Upvote 0

richard blaine

Board Regular
Joined
May 19, 2005
Messages
67
Actually I found this one which works using this order: "CountColor(A1,B1:B100)"

Function CountColor(rColor As Range, rSumRange As Range)
Dim rCell As Range
Dim iCol As Integer
Dim vResult

iCol = rColor.Font.ColorIndex

For Each rCell In rSumRange
If rCell.Font.ColorIndex = iCol Then
vResult = vResult + 1
End If
Next rCell

CountColor = vResult
End Function
 
Upvote 0

Forum statistics

Threads
1,191,226
Messages
5,985,373
Members
439,961
Latest member
drose1105

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
Top