Table of Contents
The capability to swiftly and precisely count cells based on their formatting color in Excel is a critical skill for any proficient user or data analyst dealing with complex datasets. While Excel offers numerous built-in functions for counting (like COUNTIF or COUNTIFS), none of them natively support counting based on cell background color or font color. This limitation often necessitates a custom solution, especially when dealing with data that has been visually categorized or highlighted to indicate status, urgency, or performance.
Relying solely on visual inspection to tally colored cells is inefficient and highly prone to error, especially in spreadsheets containing hundreds or thousands of rows. Fortunately, Microsoft Excel provides a robust scripting environment known as Visual Basic for Applications (VBA), which allows users to extend Excel’s functionality far beyond its standard feature set. By leveraging VBA, we can create a custom function—often referred to as a Macro—that specifically targets and counts cells based on their unique color index properties.
This powerful technique transforms qualitative visual distinctions into quantifiable metrics. For instance, you might use color coding to categorize project statuses (red for delayed, green for complete, yellow for in-progress). A custom count-by-color Macro allows you to instantly generate reports on how many projects fall into each status category, providing a rapid, accurate overview of operational performance and helping decision-makers identify critical trends or resource bottlenecks without manual effort. This approach significantly enhances data processing efficiency and accuracy when handling large, visually categorized data sets.
Setting Up the Scenario: Example Data Entry
To illustrate the process of counting cells based on color, we will use a representative dataset that mimics real-world categorization needs. Imagine a list of sales transactions or inventory items where the quantity column has been conditionally formatted or manually highlighted to reflect certain thresholds or issues. The goal is to determine the frequency of each color occurring within that range.
Our first step involves entering the sample data values into a new worksheet in Excel. For this demonstration, we are working with a single column of values where the cells have been manually or conditionally shaded with light green, light blue, and light orange backgrounds. This scenario provides the basis for our custom counting solution.

Although implementing this solution requires writing a short function using VBA (Visual Basic for Applications), users who are new to programming should not be deterred. This method bypasses complex formulas and provides a reusable, highly effective tool. The subsequent steps detail the precise, straightforward process required to activate Excel’s developer tools and deploy the necessary code.
Enabling Advanced Functionality: Displaying the Developer Tab
The customized function we are about to create utilizes VBA, which is accessed through the Developer tab in the Excel Ribbon. By default, this tab is often hidden to simplify the user interface for general users. Therefore, the first technical requirement is to make this crucial tab visible, granting access to the Visual Basic Editor (VBE) where we will define our custom counting function.
To activate the Developer tab, navigate to the backstage view by clicking the File tab located in the upper-left corner of the application window. From the menu that appears, select Options. This action opens the Customize Ribbon dialog box, which contains extensive configuration settings for the Excel environment.
Within the Options window, click on the Customize Ribbon category in the left navigation pane. On the right side of the dialog box, under the section labeled Customize the Ribbon (or Main Tabs), locate the option for Developer. Ensure the checkbox next to Developer is checked. Once confirmed, click OK to save the changes and close the options window. The Developer tab should now be visible on your main ribbon interface, positioned usually toward the right end.

Initiating the VBA Editor and Module Creation
With the Developer tab active, the next critical step is to access the Visual Basic Editor (VBE) to begin writing our custom function. The VBE is the integrated development environment (IDE) used for creating and managing VBA code within Excel. To open the editor, locate the Developer tab on the Ribbon and click the Visual Basic icon, usually the first option on the far left. Alternatively, you can use the keyboard shortcut Alt + F11.
The VBE opens in a separate window, ready for code input. Before writing the function, we must insert a Module. A module acts as a container for custom procedures and user-defined functions that can be called from any worksheet in the current workbook. To insert a standard module, navigate to the menu bar within the VBE, click the Insert tab, and then select Module from the resulting dropdown list.

This action opens a blank code window, typically titled “Module1,” where the VBA code will reside. It is within this module that we will define the parameters and logic for our specialized counting function, ensuring it interacts correctly with Excel’s cell properties, specifically the interior color index.

Implementing the Custom Function Code
The core of this solution lies in defining a user-defined function (UDF) using VBA. This function, which we will name CountByColor, accepts two main arguments: the range of cells we want to check (CellRange) and a single cell whose color serves as the reference standard (CellColor). Carefully copy and paste the following code block directly into the newly created module window.
Function CountByColor(CellRange As Range, CellColor As Range) Dim CellColorValue As Integer Dim RunningCount As Long CellColorValue = CellColor.Interior.ColorIndex Set i = CellRange For Each i In CellRange If i.Interior.ColorIndex = CellColorValue Then RunningCount = RunningCount + 1 End If Next i CountByColor = RunningCount End Function
Ensure the code is pasted accurately, as any typographical error will prevent the Macro from executing properly or appearing as a function within the spreadsheet interface. This code defines variables to hold the reference color index and a cumulative count. It then iterates through every cell in the defined range, comparing the interior color index of each cell against the reference color. If the indexes match, the counter increments.
Once the code is entered, the VBE automatically compiles the function. You do not need to press a “save” button within the VBE itself, but you must ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) to preserve the VBA code. After confirming the code entry, you can close the Visual Basic Editor. The function is now accessible like any standard Excel formula in your workbook.

Executing the Macro: Applying the Custom Function
With the CountByColor function successfully defined in the VBA module, we can now return to the worksheet and apply this new custom formula to calculate the cell counts. This step requires setting up a separate area on the sheet where you define the colors you want to count and where the results will be displayed. This structure simplifies the process of referencing the color criteria.
Begin by setting up your criteria range. In our example, we will use cells C2 through C4 to hold the distinct colors we wish to count. It is crucial that you manually format these cells (C2, C3, C4) with the exact same background fill colors—light green, light blue, and light orange—as those found in your data range (A2:A11). These cells serve as the color input arguments for our custom function.
Next, select the cell where you want the first count result to appear (cell D2 in this example). Enter the following formula, which calls the user-defined function and specifies both the data range and the reference color cell:
=CountByColor($A$2:$A$11, C2)
In this formula, $A$2:$A$11 is the absolute reference to the data range being analyzed, and C2 is the relative reference to the color criteria cell (light green). Using absolute references for the data range (by including the dollar signs) is essential, as it ensures that the range remains constant when the formula is copied down to count other colors.
Interpreting the Results and Handling Edge Cases
After entering the formula into cell D2, simply use the fill handle (the small square at the bottom-right corner of the selected cell) to drag the formula down to cells D3 and D4. Since we used an absolute reference for the data range ($A$2:$A$11) and a relative reference for the color cell (C2), the formula in D3 will automatically adjust to =CountByColor($A$2:$A$11, C3), and D4 will use C4, allowing the Macro to calculate the count for each reference color efficiently.
The resulting counts demonstrate the functionality of the custom function, providing accurate totals based on the background color properties of the cells in the target range. Based on the provided example, the following quantifiable results are immediately available:
- The count of cells with a light green background is 3.
- The count of cells with a light blue background is 4.
- The count of cells with a light orange background is 3.
This automated count eliminates manual summation errors and provides a powerful way for the data analyst to summarize visually categorized data.

A crucial consideration when using color-based counting is handling colors that do not exist within the lookup range. If you specify a cell in column C that is formatted with a color not present in A2:A11, the CountByColor function will correctly assess that no cell matches the reference color index and will simply return a value of 0. Furthermore, note that this VBA function only works by comparing the internal ColorIndex property; it does not automatically update if you manually change a cell’s color after the formula has been executed. You must force a recalculation (e.g., by editing a formula or pressing F9) to refresh the color counts.
Technical Deep Dive: Understanding the VBA Logic
Understanding the underlying logic of the CountByColor function is beneficial for anyone looking to adapt this Macro for other purposes, such as counting based on font color or conditional formatting properties. The function is defined using the Function keyword, making it callable directly from the worksheet. It receives two Range objects as input: the range to analyze (CellRange) and the cell providing the color reference (CellColor).
Inside the function, two variables are dimensioned: CellColorValue As Integer stores the numerical index of the reference color, and RunningCount As Long serves as the cumulative tally. The line CellColorValue = CellColor.Interior.ColorIndex is the most critical step, as it extracts the unique color index number from the reference cell. Excel uses a specific system for internal color indexing, and comparing these index numbers is far more reliable than comparing color names or RGB values.
The primary logic is handled by a standard VBA iterative construct: the For Each...Next loop. This loop systematically processes every single cell (represented by the variable i) within the specified CellRange. Inside the loop, the If i.Interior.ColorIndex = CellColorValue Then statement checks if the current cell’s interior color index matches the predefined reference color index. If the condition is met, the RunningCount variable is incremented by one. Finally, the total accumulated count is assigned back to the function name (CountByColor = RunningCount), which returns the result to the calling spreadsheet cell.
Cite this article
stats writer (2025). Count Cells by Color in Excel (With Example). PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/count-cells-by-color-in-excel-with-example/
stats writer. "Count Cells by Color in Excel (With Example)." PSYCHOLOGICAL SCALES, 17 Nov. 2025, https://scales.arabpsychology.com/stats/count-cells-by-color-in-excel-with-example/.
stats writer. "Count Cells by Color in Excel (With Example)." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/count-cells-by-color-in-excel-with-example/.
stats writer (2025) 'Count Cells by Color in Excel (With Example)', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/count-cells-by-color-in-excel-with-example/.
[1] stats writer, "Count Cells by Color in Excel (With Example)," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, November, 2025.
stats writer. Count Cells by Color in Excel (With Example). PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.
