VBA macros · Formatting

VBA function to count cells by background color in Excel

A user-defined function, CountByColor, that counts the cells in a range whose fill colour matches a sample cell. Use it in any formula.

Windows + Mac

Colours are a popular way to mark status: green for paid, red for overdue, yellow for check. Excel has no built-in function that counts by colour, so the usual answer is a filter by colour and a manual count. This short user-defined function adds =CountByColor(range, sample cell) to your workbook and counts the cells whose fill colour matches the sample.

VBA
Option Explicit

Function CountByColor(dataRange As Range, colorCell As Range) As Long
    ' Counts the cells in dataRange whose fill colour equals the fill of colorCell
    Dim c As Range, rng As Range
    Dim target As Long

    Application.Volatile                 ' recalculate with every calculation
    target = colorCell.Cells(1, 1).Interior.Color

    ' Ignore the empty part of whole-column references such as A:A
    Set rng = Intersect(dataRange, dataRange.Worksheet.UsedRange)
    If rng Is Nothing Then Exit Function

    For Each c In rng.Cells
        If c.Interior.Color = target Then CountByColor = CountByColor + 1
    Next c
End Function
Count cells by fill colour

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module and paste the code. A function must live in a normal module, not in a sheet module or ThisWorkbook.
  3. Go back to Excel and type the function into a cell like any built-in function, for example =CountByColor(B2:B50, E2). It also appears in the function list when you start typing =CountByColor.
  4. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

A function stored in PERSONAL.XLSB works too, but then every formula has to call it as =PERSONAL.XLSB!CountByColor(...), and the formula breaks when you send the file to someone else. For functions it is usually better to keep the code inside the workbook that uses it, or to save it as an add-in (.xlam). See User-defined functions (UDFs): your own worksheet functions for more about writing your own functions.

How it works

A Function in a normal module can be used in worksheet formulas, just like COUNTIF. The first argument is the range to check, the second a cell that has the colour you want to count. Using a sample cell is easier than typing colour numbers: colour a cell next to the formula and point to it.

Interior.Color returns the fill colour as a number. The function reads the sample's colour once, then loops through the cells and adds 1 every time a cell's colour is the same. Intersect with the used range means =CountByColor(A:A, E2) does not loop through a million empty cells.

Application.Volatile tells Excel to recalculate the function whenever anything on the sheet is recalculated. That is needed because changing a cell's colour is not a change Excel tracks for formulas. Even so, colouring a cell does not trigger a recalculation by itself: press F9 (Mac: Fn+F9 or Cmd+=) afterwards to update the count. More about writing functions in User-defined functions (UDFs): your own worksheet functions.

Change it

To count by font colour instead of fill colour, change both Interior.Color lines to Font.Color. To count the cells that have no fill, point the sample at a cell with no fill: an unfilled cell reports white (16777215), so cells explicitly filled white are counted as well.

If you want to count coloured cells that also meet a condition, for example red cells that say Open, combine both tests in the If line: If c.Interior.Color = target And c.Value = "Open" Then. To add up values instead of counting cells, use the companion function VBA function to sum cells by background color in Excel.

For a one-off count without code, filter the column by colour (Data › Filter, then Filter by Color) and use =SUBTOTAL(103, B2:B50), which counts only visible cells.

What can go wrong

Watch out. Colours from conditional formatting are not seen. They are drawn on top of the cell and do not change Interior.Color. The property that sees them, DisplayFormat, does not work inside a function called from a worksheet. Count with the same condition the rule uses instead, for example =COUNTIF(B2:B50, "<0").

Two colours that look the same can differ by one shade, for example a theme colour with a tint and a standard colour; they are then not counted together. Use the sample cell's exact fill (copy it with the Format Painter). Volatile functions recalculate often, so hundreds of them over large ranges can slow a workbook down.

Tip. Works on Windows and Mac. The file must be saved as .xlsm and macros enabled; otherwise the formulas show #NAME?.

Related: VBA function to sum cells by background color in Excel, VBA macro to highlight duplicate values in Excel, User-defined functions (UDFs): your own worksheet functions.

More vba macros