Have you ever got into situation in office where you need to count the cells with specific color in conditional formatted Excel sheet? If yes then you can use following code which counts the number of cells with specific color (here it is yellow) and put the count in cell
Public Sub CountColorCells()
'Variable declaration
Dim rng As Range
Dim lColorCounter As Long
Dim rngCell As Range
'Set the range
Set rng = Sheet1.Range("A2:A11")
'loop throught each cell in the range
For Each rngCell In rng
'Checking Yellor color
If Cells(rngCell.Row, rngCell.Column).DisplayFormat.Interior.Color = RGB(255, 255, 0) Then
lColorCounter = lColorCounter + 1
End If
Next
'Display the value in cell A12
Sheet1.Range("A12") = lColorCounter
End Sub
It is worth to mention that DisplayFormat.Interior.Color works only on Excel 2010 or above. Also the above code does not count the cells where cell color is manually changed by user. You can use .Interior.Color to get the cell colors or read this post.
To use this code in your Excel file, follow below steps:
1. Open the Excel file where you want to count the color cells
2. Press Alt+F11
3. Insert a Module (Insert>Module) from menu bar
4. Paste the code in the module
5. Now add a shape in Excel sheet
6. Give a name to the shape like ‘Refresh Count’
7. Right click on the shape and select ‘Assign Macro…’
8. Select CountColorCells from the list and click on ‘Ok’ button
9. Done
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:
1. Select the cell which contain the color you want to count
2. Right click and select ‘Format Cells…’
3. In the Format Cells dialog box go to ‘Fill’ tab and click on ‘More Colors…’
4. 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.
Microsoft Excel “TODAY” function is used to get the current Date. It is very useful function and can be used in many ways. “TODAY Function” does not have any argument that makes this easy to apply and implement.
In this post, you’ll learn how to Show formulas in Excel spreadsheets. Usually, when you create a formula in Excel, it shows the result in the cell. But sometimes, you might want to see the…
Excel Function REPLACE REPLACE function is used to replace the existing text from a specific location in a cell to New Text. REPLACE Function has argument four arguments i.e. old_text, start_num, num_chars and new_text. We need to give the…
Microsoft Excel “NOW” function is used to get the current Date and Time. It is very useful function and can be used in many ways.
Time & Motion Tracker is an MS Excel based tool which helps you to track Start and End time of any type of transaction or activity. The tool is developed using VBA coding which helps you to protect manual manipulation in the data by the user. It is also easy to use, just click on Start (shortcut: Ctrl+Shift+A) or Stop (Ctrl+Shft+S) buttons to record the time stamp.
VBA Code to Convert MM.DD.YYYY To DD.MMM.YYYY in Excel In different parts of the world, there are different languages spoken and written. With this, a VBA programmer also faces language related issues while writing a…
Any way to make this a function not a sub
Hi Natasha,
It is not possible to access Interior property of Cells in Excel when using the code as Excel Function / Formula; hence the code needs to be manually called using a button.
Regards
ExcelSirJi Team
Is there a way to span this macro to be applied to 25 different sheets in a workbook?
Hi Bob,
You can make use of following code to loop through each worksheet in the workbook and count the number of cells with conditional formatting.
Hi There,
I want to count the conditionally formatted RED colored cells but its not working…
Range : D40 to D70
Sample Cell : K40
Total Cells : L40