Count Cells by Color in Excel: 3 Real Methods

How-to basics · Updated November 23, 2026

The direct answer: Excel has no built-in COUNTIF function that counts cells by their fill color. You must use one of three workarounds: filter by color and use SUBTOTAL, write a small VBA user-defined function, or (in Excel for Microsoft 365) use the new CELL function with the “color” info type. The fastest no-code method is the filter + SUBTOTAL approach, which works in every Excel version.

Why Excel Can’t Count Colors Natively

Excel’s COUNTIF, COUNTIFS, and SUMPRODUCT functions work with cell values, not formatting. Fill color is a display property stored separately from the cell’s content. When you change a cell’s background color, Excel does not recalculate any formulas, and the standard formula engine simply has no way to read a color. This is a deliberate design choice: formatting is meant to be visual, not data. The only built-in function that even partially understands color is CELL, and only in Excel for Microsoft 365 (and Excel 2024) does it return a color code.

Method 1: Filter by Color + SUBTOTAL (No Code)

This is the simplest method and works in every Excel version. It uses the filter dropdown’s “Filter by Color” option combined with the SUBTOTAL function, which ignores hidden rows.

  1. Select your data range (e.g., A1:A20 with headers).
  2. Go to Data > Filter (or press Ctrl+Shift+L on Windows, Cmd+Shift+L on Mac).
  3. Click the dropdown arrow in the header cell of the column that contains the colored cells.
  4. Choose “Filter by Color” and select the specific fill color you want to count.
  5. In a cell outside the filtered range, enter: =SUBTOTAL(3, A2:A20) (the 3 means COUNTA, counting non-empty cells).

Example: If A2:A20 contains 10 yellow cells and 9 blue cells, after filtering yellow, SUBTOTAL returns 10. When you clear the filter, it returns 19. The 3 in SUBTOTAL counts non-empty cells; use 2 for numbers only, or 103 for COUNTA that ignores manually hidden rows.

Note: This method only counts cells you can see. If some rows are hidden for other reasons (e.g., you filtered another column), those cells are excluded. It also requires you to manually re-filter each time you want a different color.

Method 2: VBA User-Defined Function (Best for Recurring Use)

If you need to count colors frequently, a small VBA function is the most flexible. It works in all Excel versions that support macros (Windows and Mac).

To add the function:

  1. Press Alt+F11 to open the VBA editor (on Mac, use Option+F11).
  2. Go to Insert > Module.
  3. Paste the code below.
  4. Close the editor and save the workbook as a macro-enabled file (.xlsm).
Function CountByColor(CountRange As Range, ColorCell As Range) As Long
    Dim cell As Range
    Dim targetColor As Long
    Dim count As Long
    
    ' Get the fill color of the reference cell
    targetColor = ColorCell.Interior.Color
    count = 0
    
    ' Loop through each cell in the count range
    For Each cell In CountRange
        If cell.Interior.Color = targetColor Then
            count = count + 1
        End If
    Next cell
    
    CountByColor = count
End Function

Usage: With your data in A2:A20 and a reference cell (say C1) filled with the color you want to count, enter =CountByColor(A2:A20, C1). The function returns the number of cells in A2:A20 that exactly match C1’s fill color.

Important: Always save a copy of your workbook before running any macro, because macros cannot be undone with Ctrl+Z. Also, the function only counts solid fill colors; it ignores gradient or pattern fills.

Method 3: The New CELL Function (Microsoft 365 Only)

Excel for Microsoft 365 (and Excel 2024) introduced a new info type for the CELL function that can return a color code. The syntax is:

=CELL("color", A1)

This returns 1 if the cell is formatted with color for negative values, and 0 otherwise. Unfortunately, this does not return the actual fill color – it only tells you whether the cell uses color for negative numbers. So this is not useful for counting arbitrary fill colors. As of this writing, there is still no built-in function that returns the fill color of a cell. The CELL function’s “color” info type is often misunderstood; it refers to the font color for negative numbers, not the background fill.

How to Choose the Right Method

  • One-time quick count: Use filter + SUBTOTAL. No setup, works everywhere.
  • Frequent counting across many colors: Use the VBA function. It’s a one-time setup and then you can reuse it like a normal function.
  • You cannot use macros (e.g., on Excel for the web or shared workbooks): Filter + SUBTOTAL is your only option. The VBA function won’t work in Excel for the web.
  • You need to count colors dynamically as data changes: The VBA function recalculates when the referenced cells change, but not when you change a fill color. You must press F9 or re-enter the formula to refresh.

Common Problems and Misconceptions

“Why does my COUNTIF with a color condition not work?” Because COUNTIF accepts criteria as text or numbers, not colors. You cannot write =COUNTIF(A:A,"yellow").

“The VBA function doesn’t update when I change a color.” VBA functions that read cell formatting are not volatile, meaning Excel doesn’t know to recalculate them when formatting changes. You can force a recalculation by pressing Ctrl+Alt+F9 (Windows) or Cmd+Option+F9 (Mac), or by adding Application.Volatile at the top of the function, but that slows down large workbooks.

“My counts are off because of conditional formatting.” The VBA function reads the cell’s Interior.Color, which is the actual fill color, not the result of conditional formatting. If a cell appears colored because of a conditional formatting rule, the function will not see that color. There is no built-in way to count cells based on conditional formatting colors without complex VBA that reads the DisplayFormat property.

Alternatives to Counting by Color

If you find yourself constantly counting by color, it’s often a sign that the color itself should be a data value. Consider adding a helper column with a label or code that represents the color (e.g., “Red”, “Blue”). Then you can use =COUNTIF(A:A,"Red") normally. This is more robust and recalculates automatically. For example, if you have a column of statuses where red means “overdue”, add a column with the word “Overdue” and count that instead.

Frequently Asked Questions

Can I count cells by font color instead of fill color? Yes, but only with VBA. Modify the function to use cell.Font.Color instead of cell.Interior.Color. The filter method only works for fill color, not font color.

Does this work in Google Sheets? No. Google Sheets has no built-in way to count by color, and it doesn’t support VBA. The filter + SUBTOTAL method works in Google Sheets (using the filter by color option and the SUBTOTAL function), but the VBA method does not.

How do I count cells with multiple colors at once? You can use multiple SUBTOTAL formulas with different filters, or in VBA, loop through a list of reference colors and sum the results. For example, to count red and blue, use =CountByColor(A2:A20,C1)+CountByColor(A2:A20,C2) where C1 and C2 are red and blue.

Why does my SUBTOTAL return 0 when I filter by color? Make sure you are using the correct function number (3 for COUNTA) and that the range includes the header row? Actually, the range should be the data rows only. Also, ensure the cells are not truly empty – if they contain spaces or formulas that return “”, they still count as non-empty.

Is there a way to count colors without macros in Excel for the web? No. Excel for the web does not support VBA, and the CELL function’s “color” info type is not available there. Your only option is the filter + SUBTOTAL method, which works in the browser.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.