It is worth to mention that the above code does not count the cells where colors are coming through conditional formatting. You can use DisplayFormat.Interior.Color to get the conditional formatting colors (DisplayFormat.Interior.Color works only on Excel 2010 or above) or read this post
Public Function CountColorCells(rng As Range) As Long
'Variable declaration
Dim lColorCounter As Long
Dim rngCell As Range
'loop throught each cell in the range
For Each rngCell In rng
'Checking Yellor color
If Cells(rngCell.Row, rngCell.Column).Interior.Color = RGB(255, 255, 0) Then
lColorCounter = lColorCounter + 1
End If
Next
'Return the value
CountColorCells = lColorCounter
End Function
To use this code in your Excel file, follow below steps:
Here I have counted the cells with yellow color. You can change RGB (#,#,#) code to count other colors. You can find RGB codes of any color using following steps
That’s all, in the ‘Color’ dialog box, you can view the RGB (Red, Green, Blue) codes of the color
If you are look for a code to sum the cells based on it’s color then you can read this post.
In this tutorial, I will show you three ways to count colored cells in Excel (with and without VBA):
An ultimate guide for basic user to understand Excel Vlookup function. VLOOKUP is a vertical lookup which helps the user to extract the values from other columns (leftmost) basis on matching column string.
MATCH function performs lookup for a value in a range and returns its position sequence number as output. It has two required and one optional arguments
INDIRECT function is used to convert the text/string into cell reference. Function provides output as the value of that cell reference.
LEN function is used for counting number of characters in available string. The output of the function returns the count in new cell.
AVERAGEIFS function is used to get the “average” of values for matching criteria across range. Average = Sum of all values / number of items.
TRIM function is used to remove the additional spaces (i.e. spaces before/after/between the words) except for single space between words.
It doesn’t work with conditional formatting because the cell color does not really change
This code does not work with conditional formatting however we made it work. So please read below article to count colored cells with conditional formatting.
Count Colored Cells with Conditional Formatting
Hope you like the article. Please comment if you liked the solution. 🙂
Happy Reading!