Counting ROWS by color

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
First thing this morning, I was asked if there was a way to count by colors, which I know there are having seen it on this board many times :)

However, he wants to know how many ROWS have text of a certain color--not the number of cells. I can't figure out how to update any of the codes that I've found to do so.

He needs to have it count how many rows of each color, not just one specific one. So how many rows have blue text, how many have green text, etc.

I found this on Chip Pearson's site, but like I said, I don't know how to change it for what he needs:

The following function will return the number of cells in a range that have either an Interior (background) or Font of a specified color. InRange is the range of cells to examine, WhatColorIndex is the ColorIndex value to count, and OfText indicates whether to return the ColorIndex of the Font (if True) or the Interior (if False).

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

You can call this function from a worksheet cell with a formula like
=COUNTBYCOLOR(A1:A10,3,FALSE)

Any ideas?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Kristy, one question...

When you say 'Entire Row', the row must have the same font color or each cell in the row can have a different color ?
 
Upvote 0
Not so short and sweet, but seems to work:

Code:
Function CountRowsByColor(InRange As Range, WhatColorIndex As Integer, Optional OfText As Boolean = False) As Long
' This function return the number of rows in InRange with
' a background color, or if OfText is True a font color,
' equal to WhatColorIndex.
    Dim Rng As Range
    Dim r As Long
    Dim Found As Boolean
    Application.Volatile True
    r = InRange.Cells(1, 1).Row
    Found = False
    For Each Rng In InRange.Cells
        If Rng.Row = r Then
            If Found = False Then
                If OfText = True Then
                    If Rng.Font.ColorIndex = WhatColorIndex Then
                        CountRowsByColor = CountRowsByColor + 1
                        Found = True
                    End If
                Else
                    If Rng.Interior.ColorIndex = WhatColorIndex Then
                        CountRowsByColor = CountRowsByColor + 1
                        Found = True
                    End If
                End If
            End If
        Else
            r = Rng.Row
            Found = False
        End If
    Next Rng
End Function
 
Upvote 0
Juan Pablo González said:
Kristy, one question...

When you say 'Entire Row', the row must have the same font color or each cell in the row can have a different color ?

From what I gathered, it's each cell in the row has the same font color.

I haven't gotten the file from him yet to try out your suggestion, Andrew. I'll let you know how it goes :)
 
Upvote 0
Ok...heeeeeeeelp me, I'm stupid.


A little backstory: I've got the file now, and it looks like the only colors used are green, blue, pink, red, and black. And the current range is A2:BD759. I'm assuming this file will continue to have rows added to it.

Anyway, I'm trying to test Andrew's code, but I can't remember how to call a function. I tried using =COUNTBYCOLOR(A2:BD759,3,FALSE) that is on Chip's page, and even tried making a module to call the function, but I can't get anything to work.

The module I tried was just:

Sub CountColors()

Call CountRowsByColor

End Sub

but it says 'sub or function not defined' and I've got no clue how to fix it. :confused:
 
Upvote 0
Ah...that would help I guess :rolleyes:

I had put the function in ThisWorkbook, AND forgot to add "rows" to the formula.

But it doesn't seem to be working...it just gives me an answer of 0.

Plus, how could I change this so that it gives me a breakdown for each color?

I just now was asked to make a chart for this as well. That, I should be able to do, but I need the count for each color so I can average them out.
 
Upvote 0
Kristy, can I suggest a different approach ?

Use this function:<font face=Courier New><SPAN style="color:#00007F">Function</SPAN> GETCOLOR(Rng<SPAN style="color:#00007F">As</SPAN> Range,<SPAN style="color:#00007F">Optional</SPAN> Text<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Boolean</SPAN> =<SPAN style="color:#00007F">False</SPAN>)<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Variant</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Text<SPAN style="color:#00007F">Then</SPAN>
        GETCOLOR = Rng.Font.ColorIndex
    <SPAN style="color:#00007F">Else</SPAN>
        GETCOLOR = Rng.Interior.ColorIndex
    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Function</SPAN></FONT>

like this:
newLeaseExamples.xls
ABCDEFGHI
1Color
2111111-4105Automatic
31111113Red
4111111-4105
51111110Null(Differentcolors)
6111111-4105
7111111-4105
81111116Yellow
9111111-4105
101111115Blue
Hoja1


then, you can use the column I to generate a Pivot Table. The result in row 5 is because of the question I made first... if the range that the function checks has different colors, the function returns Null.

I think this approach is better than counting each different cell in the spreadsheet.
 
Upvote 0
Ok, that one's working.

I've never made a pivot table before, though, but I'm not really sure if I need one.

I've got what number = what color now using the function, couldn't I just do a COUNTIF to get how many of each color at this point?

Edit: Like so?
test.xls
ABCD
3ColorIndex
4GREEN=JUDDER/GREASE10499
5BLUE=REOPEN/HOUSING5102
6RED=SWITCH/COVER326
7PINK=MOTOR/WIRING787
8BLACK=OTHER/CLICKING143
Sheet2
 
Upvote 0

Forum statistics

Threads
1,221,701
Messages
6,161,381
Members
451,700
Latest member
Eccymarge

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