As Microsoft is releasing new versions of Excel after every few years, they are working on adding more and more features in the application to support increased user demands. There are hundreds of formulas/functions available in Excel which can be used to get any type of information from the data.
Have you ever felt that Microsoft should have added a formula in Excel which can count the cells based on specific color? I have seen many code requests to share a VBA code that can count the cells by its color.
To help our subscribers and developers, we are sharing 2 codes that be used to count the cells with specific color and returns the count of the matching color cells.
Public Function CountByColor(rng As Range, Red As Long, Green As Long, Blue As Long) As Long
'Variable declaration
Dim lCount As Long
Dim rngCell As Range
'Loop throught each cell in the range
For Each rngCell In rng
'Checking and count color
If rngCell.Interior.Color = RGB(Red, Green, Blue) Then
lCount = lCount + 1
End If
Next
'Return the value
CountByColor = lCount
End Function
Public Function CountByColor(rng As Range, ColorCell As Range) As Double
'Variable declaration
Dim lCount As Long
Dim rngCell As Range
'Loop throught each cell in the range
For Each rngCell In rng
'Checking and count color
If rngCell.Interior.Color = ColorCell.Interior.Color Then
lCount = lCount + 1
End If
Next
'Return the value
CountByColor = lCount
End Function
If you want to use this code in your VBA tool, then follow below steps:
Step 1: Open the Excel file in which you want to copy this code
Step 2: Press Alt+F11 to open VBA editor
Step 3: Insert a new module from Insert > Module menu
Step 4: Paste the code in the module
Step 5: Now you are ready to use this code as Excel Function/Formula
Step 6: Type the formula in the cell where you want to get the count of specific colored cells
First Code Example:=CountByColor(A2:A11,146, 208, 80)
The first parameter of the formula is the range from which you want to count the specific color cells. The second, third and fourth parameters are RGB code of the color.
Second Code Example:=CountByColor(A2:A11,A8)
The first parameter of the formula is the range from which you want to count the specific color cells. The second parameter is the cell from which you want to compare the color.
In the first VBA code, we have used RGB color codes to find the matching cells and sum the values. You can find RGB codes of any color using following steps:
Step 1: Select the cell which contain the color you want to use
Step 2: Right click and select ‘Format Cells…’
Step 3: In the Format Cells dialog box go to ‘Fill’ tab and click on ‘More Colors…’
Step 4: That’s all, in the ‘Color’ dialog box, you can view the RGB (Red, Green, Blue) codes of the color
Thanks for reading the article, subscribe us to get more VBA tricks
VBA Code to check if folder exist Validation is one of the important parts of any programming language. As per few studies, 60% of the code is focused on validating input or output. In this…
This step-by-step guide will show you how to easily do mail merge in Word using an Excel sheet.
Mail Merge can save you a lot of time when you need to send letters or emails to many people. It helps you create personalized letters, emails, or labels in Word by combining the information from your Excel sheet. This guide gives an overview of the main steps and explains how to do a mail merge from Excel in an easy way
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
Index Match in Excel are powerful tools for advanced lookups. They’re popular because they’re incredibly flexible. You can use them to search horizontally or vertically, perform two-way searches, find values to the left of your search criteria, handle case-sensitive searches, and even search using multiple conditions. If you want to improve your Excel skills, mastering INDEX and MATCH is essential. Below, you’ll find numerous examples to help you grasp how they function.
This tutorial introduces XLOOKUP, a new function in Excel for both vertical and horizontal lookups. Tasks that used to feel super complicated, like left-side lookups, finding the last match, or using VLOOKUP with multiple criteria, are now much easier with XLOOKUP.
Before, you had to choose between VLOOKUP for vertical lookups, HLOOKUP for horizontal ones, or more complex options like INDEX MATCH or Power Query. But now, you don’t have to pick anymore. XLOOKUP can handle all those tasks in one simple function.
Excel has built-in options to make graphs, it doesn’t have a heat map feature. However, you can quickly and easily create a heat map in Excel using conditional formatting.
This helped heaps thank you very much! I now I am wondering how do I write up a VBA code to auto-update or simultaneously count cells by color when I am doing color coding my cells. Cheers
Place the code in Worksheet change event (the sheet) where you want to use this.