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.
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.
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 FunctionHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module and paste the code. A function must live in a normal module, not in a sheet module or
ThisWorkbook. - 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. - 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
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.
#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.