How to Count Colored Cells in Excel (Using COUNTIF): A Step-by-Step Guide

If you’ve ever needed to count colored cells in Excel, you might have found it tricky. Unfortunately, Excel doesn’t offer a direct formula for counting cells based on color. But don’t worry, you can use a combination of functions, including the COUNTIF function, along with some VBA magic, to get it done. Here’s a step-by-step guide to help you out.

How to Count Colored Cells in Excel Using COUNTIF

We’ll be using a mix of a custom VBA function and Excel’s COUNTIF. This process might seem a little complex, but by following these steps, you’ll be able to count the colored cells in no time.

Step 1: Open the Visual Basic for Applications Editor

First things first, we need to create a custom function using VBA.

To open the VBA Editor, press Alt + F11. This will bring up a new window where you can write VBA code.

Step 2: Insert a New Module

Next, we need to create a space to write our custom function.

In the VBA Editor, click Insert and then select Module. This will open a blank module where we can write our code.

Step 3: Write the VBA Code

Now it’s time to write the code that will help us count the colored cells.

Copy and paste the following code into your module:

Function CountColoredCells(rng As Range, cell As Range) As Long
    Dim cellColor As Long
    Dim cellCount As Long
    cellColor = cell.Interior.Color
    For Each cell In rng
        If cell.Interior.Color = cellColor Then
            cellCount = cellCount + 1
        End If
    Next cell
    CountColoredCells = cellCount
End Function

This function will count cells in a given range (rng) that have the same color as a reference cell (cell).

Step 4: Close the VBA Editor

Once you’ve added your code, you need to close the VBA Editor.

Click the X button at the top right corner of the VBA Editor to close it and return to Excel.

Step 5: Use the New Function in Excel

Now, you can use your custom function directly in Excel.

In any cell, type =CountColoredCells(A1:A10, B1), replacing A1:A10 with the range of cells you want to count and B1 with the reference cell that has the color you want to count. Press Enter to see the result.

After you complete these steps, you’ll be able to count the colored cells in your selected range.

Tips for Counting Colored Cells in Excel Using COUNTIF

  • Save Your Work: Always save your Excel file before working with VBA, just to be safe.
  • Use Descriptive Names: When writing VBA code, use descriptive names for your functions and variables to make the code easier to understand.
  • Test Your Code: Test your VBA function on a small range first to make sure it works correctly.
  • Backup Your File: Always make a backup of your file before running any VBA code, in case something goes wrong.
  • Practice Makes Perfect: If you’re new to VBA, practice writing simple functions to get comfortable with the syntax and logic.

Frequently Asked Questions

Can I use this method to count multiple colors at once?

No, this function counts cells based on one specific color. You would need to run it separately for each color.

Does this function work with conditional formatting?

This function does not account for conditional formatting. It only counts cells with a manually set color.

Will this method slow down my Excel file?

If you’re working with very large datasets, using VBA can slow down your Excel file. Use it with caution.

Do I need special permissions to use VBA?

Yes, you might need to enable macros in your Excel settings to run VBA code.

Can I use this function on a Mac?

Yes, but the steps to access the VBA Editor might be slightly different, as Excel for Mac has a different interface.


  1. Open the VBA Editor with Alt + F11.
  2. Insert a new module.
  3. Write and paste the custom VBA code.
  4. Close the VBA Editor.
  5. Use the new function in Excel.


Counting colored cells in Excel using the COUNTIF function combined with VBA might seem like a daunting task at first, but it’s quite manageable with a little practice. This powerful method can save you a lot of time and effort, particularly when dealing with large datasets. Start by familiarizing yourself with the VBA Editor and writing simple functions, then work your way up to more complex tasks like counting colored cells.

If you’re interested in learning more about VBA, there are plenty of resources online, ranging from beginner tutorials to advanced programming guides. Whether you’re a student, a business professional, or just someone looking to sharpen their Excel skills, mastering VBA can significantly enhance your spreadsheet capabilities. So, why wait? Dive in and explore the endless possibilities that VBA offers!

Get Our Free Newsletter

How-to guides and tech deals

You may opt out at any time.
Read our Privacy Policy