Formula for counting cells with Fill Color

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
That is filled by the user. A UDF could be used to count cells by their fill color.

I've found that a helper column is an easier way to mark rows than coloring cells.

This UDF will count the number of colored cells in a range.
Code:
Function CountOfColor(aRange As Range, ColorIndex As Double) As Double
    Dim i As Long
    Application.Volatile
    If ColorIndex < 1 Then ColorIndex = xlNone
    With aRange
        For i = 1 To .Cells.Count
            CountOfColor = CountOfColor - (.Item(i).Interior.ColorIndex = ColorIndex)
        Next i
    End With
End Function
 
Upvote 0
Mike hope you don't mind me stepping in here :)

Dean, Mikes code is for use in another macro or on the worksheet like this =countofcolor(A8:B12,3) where you have to stippulate the range and the colour you are counting, did you just want to count all cells with a coulour in them?
 
Upvote 0
Thanks both you guys.....Simon yours seems more of my liking.

The other seems more in the access VB area. Mine is just simple counting
fill colors.

Great Site!.....and fast responses!

Thanks Again, Dean
 
Upvote 0
You misunderstand, the formula i showed you IS Mike's, thats how you use it on the worksheet, but you want to count all coloured cells, yes?, do the coloured cells have text in them?
 
Upvote 0
Simon, feel free to help.

I'll again suggest that the plan for the spreadsheet be changed.
Colored highlights can be useful for conveying data to a human, but is not a good "data coding" technique for communicating to Excel.

Humans can easily understand that a highlighted a row means "Important".
Excel has a much easier time interpreting entries in a Level-of-Import (1, 2, 3) column than reading meaning into a color.

Debugging and maintaining a spreadsheet can get horrible if there is a "code" that Red=West Division, Blue=North, Yellow=East, etc. that needs to be remembered by users (and the file maintainer).

I strongly advise against a scheme where the color of a cell is data to be analyzed by formulas.

The UDF approach that I posted has a flaw, in that changing the fill color does not trigger the calculation of even a Volatile function.
A Change event won't work either, for the same reason.
(Another example that Excel is not built to handle color as data.)
 
Last edited:
Upvote 0
Sorry Mike/Simon, 1st day on site and just found quick reply icon.

Yes i just want to count colored cells and they will have numeric values ion them.
 
Upvote 0
Mike,

I agree, but what we have is a simple datasheet that the grader highlights areas that are out of spec. They simply highlight in yellow if out.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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