VBA Code to Count Color Cells With Conditional Formatting
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
Count Color Cells With Conditional Formatting
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
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
Download Practice File
Excel VBA Course : Beginners to Advanced
We are offering Excel VBA Course for Beginners to Experts at discounted prices. The courses includes On Demand Videos, Practice Assignments, Q&A Support from our Experts. Also after successfully completion of the certification, will share the success with Certificate of Completion
This course is going to help you to excel your skills in Excel VBA with our real time case studies.
Lets get connected and start learning now. Click here to Enroll.
Secrets of Excel Data Visualization: Beginners to Advanced Course
Here is another best rated Excel Charts and Graph Course from ExcelSirJi. This courses also includes On Demand Videos, Practice Assignments, Q&A Support from our Experts.
This Course will enable you to become Excel Data Visualization Expert as it consists many charts preparation method which you will not find over the internet.
So Enroll now to become expert in Excel Data Visualization. Click here to Enroll.
Use Automated Excel Utility Tools to increase productivity
Hope you are enjoying learning Excel with us, if you want any support related to this article, please do comment else you can ask questions in Excel Community
Any way to make this a function not a sub
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.
Is there a way to span this macro to be applied to 25 different sheets in a workbook?
You can make use of following code to loop through each worksheet in the workbook and count the number of cells with conditional formatting.