Count cells based on color (2024)

You can count cells based on color using a custom formula in Excel. In the example, we’ll show you how to do that!

Counting colored cells in Excel is difficult, as the default functions cannot access the Excel Object Model, which handles color management. Fortunately, we can quickly write a user-defined function that makes this task manageable.

How to count cells based on cell color

To count colored cells in Excel, follow these steps:

  1. Type =COUNTBYCOLOR(B2, E5:E10)
  2. Add the cell (B2) that contains the color you want to count.
  3. Select the range (E5:E10) where you want to count the cells with that color.
  4. Press Enter to see the result.

Example

The main goal of the COUNTBYCOLOR function is to count cells in a range that matches a specific color. It requires only two arguments. The first is the cell with the color to be matched. The second is the range to check. To make it clear, here’s a practical example.

In the example, the formula =COUNTBYCOLOR(B4, D2:D11) is used to count the number of cells in the range D2:D11 that have the same color as cell B4. The result is 3, indicating three cells with the same color as B4 in the specified range.

Count cells based on color (1)

COUNTBYCOLOR Function: Under the Hood

In this section, we publish the function’s source code. We have added comments to make it easier to understand. For those interested in how the function works, this is likely the most effective way to help.

Function COUNTBYCOLOR(refColor As Range, ParamArray cellRanges() As Variant) As Long Dim currentCell As Range ' Variable to iterate through each cell in the target range Dim singleRange As Variant ' Variable to hold each range in the ParamArray Dim totalMatches As Long ' Variable to store the total count of matching cells Dim matchCriteria As Integer ' Variable to determine the type of cells to count (all, non-blank, blank) totalMatches = 0 ' Initialize the total matches count to 0 matchCriteria = 0 ' Default to counting all cells ' Check if the last argument in the ParamArray is a number, which would set the match criteria If UBound(cellRanges) > 0 Then If IsNumeric(cellRanges(UBound(cellRanges))) Then matchCriteria = cellRanges(UBound(cellRanges)) End If End If ' Loop through each range provided in the ParamArray For Each singleRange In cellRanges ' Check if the current item in ParamArray is a Range If TypeName(singleRange) = "Range" Then ' Loop through each cell in the current range For Each currentCell In singleRange ' Check if the cell color matches the reference color If currentCell.Interior.color = refColor.Interior.color Then ' Decide what to count based on the match criteria Select Case matchCriteria Case 0 ' Count all cells (default) totalMatches = totalMatches + 1 Case 1 ' Count only non-blank cells If Not IsEmpty(currentCell.Value) Then totalMatches = totalMatches + 1 End If Case 2 ' Count only blank cells If IsEmpty(currentCell.Value) Then totalMatches = totalMatches + 1 End If End Select End If Next currentCell ' If the item is an array of ranges (from a multi-area range), loop through each sub-range ElseIf TypeName(singleRange) = "Variant()" Then Dim subRange As Range ' Variable to hold each sub-range within a multi-area range For Each subRange In singleRange ' Loop through each cell in the sub-range If TypeName(subRange) = "Range" Then For Each currentCell In subRange ' Check if the cell color matches the reference color If currentCell.Interior.color = refColor.Interior.color Then ' Decide what to count based on the match criteria Select Case matchCriteria Case 0 ' Count all cells (default) totalMatches = totalMatches + 1 Case 1 ' Count only non-blank cells If Not IsEmpty(currentCell.Value) Then totalMatches = totalMatches + 1 End If Case 2 ' Count only blank cells If IsEmpty(currentCell.Value) Then totalMatches = totalMatches + 1 End If End Select End If Next currentCell End If Next subRange End If Next singleRange COUNTBYCOLOR = totalMatches ' Return the total count of matching cellsEnd Function

You can download the sample file here if you do not want to install our add-in.

Final words

It is evident that in many cases, despite our best intentions, we can’t use built-in Excel formulas. In such situations, it’s worth writing a simple function using VBA. You can easily use the function you create in any Excel environment without compatibility issues.

Count cells based on color (2024)

References

Top Articles
Poughkeepsie Journal from Poughkeepsie, New York
Nys E Courts Family
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
fltimes.com | Finger Lakes Times
Detroit Lions 50 50
18443168434
Newgate Honda
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Grace Caroline Deepfake
978-0137606801
Nwi Arrests Lake County
Missed Connections Dayton Ohio
Justified Official Series Trailer
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Sizewise Stat Login
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Jet Ski Rental Conneaut Lake Pa
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Ups Print Store Near Me
C&T Wok Menu - Morrisville, NC Restaurant
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
University Of Michigan Paging System
Dashboard Unt
Access a Shared Resource | Computing for Arts + Sciences
2023 Ford Bronco Raptor for sale - Dallas, TX - craigslist
Speechwire Login
Healthy Kaiserpermanente Org Sign On
Restored Republic
3473372961
Craigslist Gigs Norfolk
Moxfield Deck Builder
Senior Houses For Sale Near Me
Trivago Myrtle Beach Hotels
Three V Plymouth
Poe Self Chill
Port Huron Newspaper
Jimmy John's Near Me Open
Greatpeople.me Login Schedule
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5303

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.