How-to recipes · Counting and summing

How to count cells by color in Excel

Excel has no COUNTIF for colours. Count coloured cells with Filter by Color and SUBTOTAL, Find All, or a short VBA function, and know each method's limits.

Intermediate SUBTOTAL COUNTIF COUNTIFS

Someone has coloured the overdue tasks red and the finished ones green, and now wants to know how many of each there are. It seems like it should be a one-click job, but Excel has no worksheet function that reads a cell's colour: COUNTIF, SUMIF and their relatives only look at values. That is true in Microsoft 365 as much as in Excel 2016. There are still several good ways to get the count, and choosing the right one depends on why the cells are coloured. This recipe explains the options and their limits honestly.

The example data

Seven tasks in A1:B8. The cells of the overdue tasks (rows 3, 5 and 7) have a red fill.

Best option: count the reason, not the colour

In most workbooks the colour means something that is also in the data: a status, a date in the past, a value over a limit. Count that directly and the result is reliable and updates automatically:

D2fx=COUNTIF(B2:B8,"Overdue")
ABCD
1TaskStatus
2Update price listDone3
3Call supplierOverdue
4Prepare invoiceOpen
5Stock countOverdue
6Team meeting notesDone
7Renew insuranceOverdue
8Website bannerOpen

Three tasks have the status Overdue, the same ones that are coloured red.

Result: 3

This is especially important when the colour comes from conditional formatting, because none of the colour-reading methods below can see conditional-format colours reliably. Count with the rule's own formula instead. For example, if the rule colours dates before today, count them with =COUNTIF(C2:C8,"<"&TODAY()). If colours are applied by hand, consider adding a Status column with a drop-down list (How to create a drop-down list in Excel) and colouring it with conditional formatting; then counting becomes trivial.

Without formulas: Filter by Color + SUBTOTAL (all versions)

  1. Below the list, in A10, type =SUBTOTAL(103,A2:A8). Function 103 counts the non-empty cells that are visible.
  2. Click inside the list and turn on the filter: Data > Filter (Ctrl+Shift+L [Mac: Cmd+Shift+F]).
  3. Open the filter arrow of the coloured column and choose Filter by Color, then pick red.
  4. Only the three red rows remain visible and A10 shows 3. Pick green to see the green count.

SUBTOTAL ignores rows hidden by the filter, which is what makes this work; COUNTA would still count all seven. For totals, use 109 (sum) instead of 103. See How to sum only visible cells in Excel.

One-off count: Find All

Press Ctrl+F, click Options > Format > Choose Format From Cell, click a red cell, then Find All. The status line at the bottom of the dialog says 3 cell(s) found. Quick, but it does not update and it only finds manually applied fill colours.

A reusable function with VBA

If you really need a live formula that counts manual fill colours, write a small user-defined function. Press Alt+F11 [Mac: Opt+F11], choose Insert > Module, and paste:

VBA
Function CountByColor(rng As Range, sample As Range) As Long
    Dim c As Range
    Dim n As Long
    Application.Volatile
    For Each c In rng.Cells
        If c.Interior.Color = sample.Interior.Color Then
            n = n + 1
        End If
    Next c
    CountByColor = n
End Function
Counts the cells in rng whose fill colour is the same as the fill of the sample cell.

Back in the sheet, colour an empty cell red as a sample, for example D5, and type =CountByColor(B2:B8,D5). It returns 3. To sum instead of count, change n = n + 1 to n = n + c.Value and declare n As Double. Save the workbook as .xlsm, or the code is lost. User-defined functions are explained in User-defined functions (UDFs): your own worksheet functions.

Watch out. Changing a cell's colour does not trigger a recalculation in Excel. After recolouring cells, press F9 to update the count. Application.Volatile makes the function recalculate whenever anything else changes, but not on a format change alone.

Limits of the VBA approach

  • It reads only manual fill colours. Colours from conditional formatting are not stored in Interior.Color, and DisplayFormat, which would show them, does not work inside a function called from a cell.
  • Macros must be enabled; colleagues opening the file with macros blocked see #NAME?.
  • Very similar shades (two slightly different reds) do not match. Use the same colour from the palette every time.
  • Excel for the web cannot run VBA, so the formula shows an error there.

Summary

MethodUpdates automaticallySees conditional formattingVersion
COUNTIF on the reasonYesYes (counts the rule's condition)All
Filter by Color + SUBTOTALWhen you change the filterYes, filtering by CF colour works2010+
Find AllNoNoAll
VBA CountByColorAfter F9NoDesktop with macros
Tip. Colour is a poor way to store information, because formulas cannot read it, it is invisible to screen readers, and it is lost when data is exported to CSV. Put the meaning in a column and let conditional formatting add the colour: How to use conditional formatting with a formula in Excel.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 12 KB

More how-to recipes