Last updated on March 13, 2025 By Victor Chan
Did you know Excel can’t count colors natively? There’s no built-in feature for it.
Yet thousands of Excel users need this skill daily. I’ve gone through endless articles, hundreds of forum comments, and dozens of videos to uncover how experts tackle this problem.
In this guide, I’ll walk you through five levels of solutions, starting simple and ending with powerful VBA code, so you can count colors like a pro.
Watch every step in my YouTube video tutorial.
Download FREE Excel Workbook
Step 1: Sign up for free → Click Here
Step 2: Log in for access → Click Here
Step 3: Download file from → This Page
(Filename: Count_Cells_by_Colors.zip)
Are you new to Excel VBA Macros?
• Find out how to enable and disable macros in Excel with this tutorial
• Save time and effort by copying VBA macros from one workbook to another with these instructions
Let’s start basic. Imagine a single column of colored cells. I want to count greens, yellows, and oranges. Here’s how.
This works for one-off color counting. But it’s tedious with lots of colors. Let’s level up!
For Level 2, we’ll use filters.
Click a cell in your data. Press Ctrl + Shift + L to add filter drop downs.
Want to count greens?
Count them manually, there’s four. Or check the status bar: “4 of 18 records found.”
Type “4” in your result cell. Clear the filter.
Repeat for yellow and orange.
It’s better than Level 1. But we can do more.
Now, let’s tackle a two-dimensional range with multiple columns. Life’s unpredictable, so let’s prepare for it.
Select your range. Go to the Insert tab and click Table, or press Ctrl + T. Confirm it has headers. Click OK. This adds dropdowns.
Next, go to Table Design and check the box for “Total Row”.
A row appears at the bottom. Click on the dropdown button and set it to Count.
Drag it right with Ctrl + R to fill.
Now filter each column using the dropdown on the header row to filter on color, and it will show the count for that color. Then you can manually enter the color counts in your result cells.
This is flexible. You can switch to Average or Sum instead of Count.
But filtering each column individually is still a hassle. Let’s fix that.
Time to supercharge this with VBA which stands for “Visual Basic for Applications”.
Don’t worry if it’s new. I’ll keep it simple. (And make sure you watch the video because it makes the whole process much easier to understand).
Let’s count colors in a Tetris grid.
Let’s say we want to count the red cells in the Tetris Grid.
In the cell for red color count (R9 in the image), type =countcolor(.
This is a User Defined Function (UDF) that we create and add to the Excel workbook. We can use it like an ordinary Excel function like SUM or XLOOKUP.
Pick a cell with the color to count (say, the red cell in Q9). We’re still editing the formula, so add a comma then select the data range (cells B8:M28 in the image).
Press Enter. Boom, our UDF counts 25 red cells.
If we want to fill a range, press F2 to edit. Select the data range (B8:M28), hit F4 to lock it ($B$8:$M$28), and fill down. All colors are counted fast.
Try counting all those colors with the methods in Levels 1 - 3 and you’ll be working for a long time! With a User Defined Function we can do it super fast.
How VBA Works: A Quick Tour
Let’s peek under the hood. Press Alt + F11 to open the Visual Basic Editor. The Project Explorer lists your project and modules. The Properties window shows the module name.
New to Excel VBA? Right-click the project or objects folder. Choose Insert > Module.
Name the module something like “Count_Color” and note that we can’t include spaces in module names, but we can use underscores, so that’s why I called the module “Count_Color” and not “Count Color”.
Our star is a User-Defined Function (UDF) called CountColor.
Here’s the code that you can copy and paste into the code window:
[VBA Code Box]
Function CountColor(rng_Ref As Range, rng_Data As Range) As Long
Dim cell As Range
For Each cell In rng_Data
If cell.Interior.Color = rng_Ref.Cells(1, 1).Interior.Color Then
CountColor = CountColor + 1
End If
Next cell
End Function
The CountColor Function Explained
The code starts with Function and ends with End Function. That marks the start and end of our User Defined Function (”UDF”).
Here’s the first line:
Function CountColor(rng_Ref As Range, rng_Data As Range) As Long
The inputs are the color cell (rng_Ref) and data range (rng_Data).
The output is a long integer (As Long).
The code inside the function checks each cell’s interior color against the reference color. If there is a match, the function adds 1 to CountColor.
Imagine counting purples in the Tetris grid. We scan each cell from the top left corner, and then move to the right so we check every cell in the first row, then move to the next row down, and so on until we’ve checked all the cells.
Our UDF does the same thing.
When it meets the first purple cell, it calculates CountColor = 0 + 1.
And the next purple cell? CountColor = 1 + 1.
By the end, you’ve got the total number of purple cells.
The UDF returns the value of CountColor to your Excel sheet.
Save It Right: Use .xlsm Files
When we use VBA, whether it’s to write a User Defined Function like above, or it’s to write a Sub procedure to carry out automations (like the one in Level 5), we need to be careful how we save the file.
Any time you have VBA in your file, you need to save it as an Excel Macro-Enabled Workbook (.xlsm or .xls), and not a regular .xlsx.
Make sure you save your file the right way or your code doesn’t get saved!
But there’s one Catch: CountColor() Won’t Update Automatically
Here’s the snag. If we change the cell colors on our sheet, CountColor() function doesn’t update to match. Even if we click Formulas > Calculate Sheet it doesn’t update.
Why? The UDF only updates if you edit it with F2 and hit Enter. Let’s figure out how to make this better.
Let’s make it user-friendly. Back to the VBA Editor (Alt + F11). Open the Level 5 module. Meet our new updated UDF “CountColor2”:
[VBA Code Box]
Function CountColor2(rng_Ref As Range, rng_Data As Range) As Long
Dim cell As Range
' Add this to make function update when sheet is recalculated
Application.Volatile
For Each cell In rng_Data
If cell.Interior.Color = rng_Ref.Cells(1, 1).Interior.Color Then
CountColor2 = CountColor2 + 1
End If
Next cell
End Function
We added a single line of code: Application.Volatile.
This updates the function on recalculation. If we don’t include it, then the color count doesn’t change even if we recalculate the sheet. When it’s there, it tells Excel that we need to recalculate the result of this function every time the sheet is recalculated.
The green line above it is a comment, so it doesn’t really count as a line of code. It’s ignored when Excel runs the VBA and it’s there so we can write reminders to ourselves or other human readers to make it easier to understand what’s going on.
Rename CountColor to CountColor2 everywhere inside the UDF because we changed the name of the function, so we need to update the code to run properly.
Let’s also add this subroutine:
[VBA Code Box]
Sub Refresh_Button()
ActiveSheet.Calculate
End Sub
This Sub procedure recalculates the active sheet. So all color count functions on the sheet will update to the correct values.
Now, go back to Excel by pressing ALT + F11.
Let’s add a refresh button to the sheet. Go to Insert > Shapes, pick a rectangle, and draw it.
Double-click to edit the text, type “Refresh,” and format the text with Ctrl + A to select all and Ctrl + B to bold it, and finally click on Home > Center to make it centered inside the rectangle.
Right-click the shape, select Assign Macro, and choose Refresh_Button. Click OK. Note: a macro is another term for VBA code that we can run.
Now you can test it. Add five red cells. Hit Refresh. Now the red cell count will increase by 5. Done!
VBA can do a lot more! Here’s pixel art of the Golden Gate Bridge from Pexels.com, made with the “Pixel Art” add-in.
Get it via Home > Add-ins > More Add-ins.
Search “PixelArt,” install it, and upload an image. It pixelates into Excel.
If the image is too large to fit on your screen you can zoom out with CTRL + ALT + Minus (repeat as many times as needed to zoom out). You can also zoom in with CTRL + ALT + Plus.
We can pick individual colors, like light blue, and use CountColor2 to count them.
But I also created more VBA in two separate modules to analyze the colors in the pixelated image in more comprehensive ways. All of the code is in the downloadable file (scroll to the top to get it).
The first Module is called “Z_Advanced_1”. It contains three procedures. Note that “GetAllCellColors” is a UDF so it can’t be run directly. Instead it needs to be used the same way as CountColor() in Levels 4 and 5.
The second Module is called “Z_Advanced_2”. It has one Sub procedure called “CreatePixelatedColorListGrouped” that groups the colors into buckets by similar hue so it’s easier to get an overview of how the colors are distributed. And there are three helper functions to do color calculations.
This is what you get when you run the Sub procedure “CreatePixelatedColorListGrouped” and choose 30 color buckets:
Take Your Excel Skills Further
Excel VBA is powerful. Want to learn it? Check out my VBA course for beginners. Automate your work and save time!
Real-World Impact of VBA
In 2011, I was working under intense pressure with a high-stakes deadline. The company’s board of directors needed updated reports for an important meeting, and I had just minutes to deliver them.
My hands were cold and clammy as I held a stack of papers like a shield outside the VP’s office. Inside, the VP—my boss’s boss—was reviewing the first version of the reports with a red pen, ready to mark up any mistakes.
Suddenly, his computer dinged with an email: there was a last-minute change that had to be reflected in the reports.
I scrambled back to my desk. There were only 30 minutes left before the meeting. I opened Excel, clicked a few buttons to run my macros, and watched as they flawlessly updated the reports in minutes.
Without VBA, this task would have taken hours, and I would have missed the deadline.
Instead, the reports were ready with 20 minutes to spare. The VP commended my quick turnaround, and I realized in that moment how much VBA had saved my job and transformed my career.
How a Chance Encounter Got me into Excel VBA
Another pivotal moment came years earlier, back when I was working at a billion-dollar tech company.
I was in the open floor office, staring at a colleague’s laptop screen during a meeting. She opened an Excel file and then clicked a button that brought up the VBA editor.
At first glance, the editor looked overwhelming, filled with a ton of code that seemed completely foreign to me.
But then she began explaining what it all meant. The macros she had written were automating hours of repetitive work, saving her time every single week.
She showed me how it helped her prepare reports by handling data preparation, formatting, and analysis with just a few clicks. These reports were critical and went straight to the company’s senior executives, including the CEO and CFO.
Her words struck me deeply: “This is the only way I can meet my Monday afternoon deadline. Without these macros, I’d never finish in time.”
And then she added, “By the way, these reports are about to become your responsibility. You’ll need to learn VBA fast if you want to succeed in this role.”
That moment was a turning point.
Suddenly, VBA didn’t look so intimidating. It looked like a powerful cheat code that could make my life easier and let me meet those critical deadlines.
From then on, I made it my mission to learn VBA, and it transformed not only how I worked but how I viewed Excel as a tool for solving problems.
Learn VBA the Right Way
The truth is that most Excel users have no idea that VBA even exists, or they see it as something that's just too hard to learn.
But I'm here to help you take that next step and learn VBA for yourself. I spent years figuring out how to teach VBA so there's nothing to stop you.
More than 500 Excel users have taken my Excel VBA course, and now they use VBA to automate their work. And that could be you in a few weeks' time.
How my VBA Course is Designed
My course has over 10 hours of video tutorials taking you from the basics of macros to doing some advanced tasks with VBA. It also includes practice files and quizzes to keep the concepts fresh and give you the much needed practice to get good at Macros and VBA.
The course is split into three levels:
In Level 1 just like teaching someone to drive a car, I start with the basics of car control and road awareness. This means recording macros and learning how to use the VBA editor to write and edit code.
In Level 2 we take the car out onto public roads. Just like a learner driver practices to handle road signs and traffic, in Excel, I show you important ways to handle macros and VBA.
And in Level 3 we jump into the fast lane and turbocharge what you learned in earlier levels. You start to make reports fly out and then automate your work based on the foundations you learned in Levels 1 and 2.
Check out my VBA Course
When you can write VBA code efficiently and confidently it will change the trajectory of your career or business.
So, if you're ready to transform the way you work in Excel and automate repetitive tasks, check out the link to my online course. And I hope to see you inside soon!
Hi, I'm Victor!
Are you struggling with complex Excel tasks? Feeling overwhelmed by spreadsheets that are hard to use?
Many people believe mastering Excel is about learning shortcuts, functions, and formulas. But this overlooks the importance of building practical, real-world applications. It's not just about knowing the tools. It's about using them effectively.
That's where I come in. You'll get a unique perspective to Excel training from me. I have over 20 years of experience at Deloitte and two global tech companies. And I know what can make a difference in your career.
Let me help you integrate Excel into your professional life. Starting today. Read one of my articles, watch one of my videos. Then apply the new technique to your work. You'll see the difference immediately!
Discover the PROVEN Blueprint for transforming your Excel skills, supercharging your productivity, and standing out in your career! My course helps you to learn Excel VBA and save hours of time even if you have zero prior experience with programming.
Solve tricky Excel problems and take your work to the next level! Get customized solutions for your unique needs. Save time and gain insights with truly expert Excel solutions from only $97 per task.
Get a clear overview of your project progress using the Excel project timeline. Use it to communicate the big picture, track task progress, and stay on top of your project goals. Stay organized with our project timeline!
Our cheat sheets provide quick and easy reference to commonly used Excel VBA concepts and code snippets.
Unlock new levels of productivity and efficiency with our cheat sheets, and write VBA code like a pro in no time.
RECOMMENDED READING
Are you looking to upskill and stay ahead of the curve? Excel is a powerful tool that keeps growing in demand. We round up the best online courses for learning Excel.
Are you looking to up your spreadsheet game? Excel is an invaluable tool that can help you stay organized and save time. From data analysis to budgets, Excel can do it all!
Today, having Excel skills is more critical than ever. Those who know how to use Excel are more likely to find higher-paying jobs. And get promoted faster.