Excel’s COUNTIF function cannot count cells by fill color on its own: its criteria are based on cell values, not formatting. To count a color, use a helper column that records the reason for the color, filter by color for a quick check, or use a VBA function for cells with manually applied fills.
Why COUNTIF Cannot Read a Cell’s Color
COUNTIF checks whether cells meet a value-based condition, such as containing a particular word, equaling a number, or matching a date criterion. A cell’s fill color is formatting, separate from its value. Excel therefore has no built-in criterion such as “green fill” that you can pass to COUNTIF.
This distinction matters because a color can have different meanings. Someone might fill a cell green by hand, while another green cell could get its color from a conditional-formatting rule. The two cells may look identical even though Excel applies their formatting in different ways. A color-based count also does not tell you why a cell has that color.
If the color represents a meaningful status—such as “Late,” “Approved,” or “Below target”—counting that status is usually more dependable than counting its appearance. It gives you a result that updates when the underlying data changes and makes the reason for the count visible.
Best Method: Count the Value Behind the Color
When a fill color represents a category, store the category in a cell and use that value as the counting criterion. You can apply color separately, including with conditional formatting, but the value remains the source of truth. This avoids errors if someone changes a fill, copies formatting incorrectly, or views the workbook with a different display setting.
For example, suppose cells B2:B8 contain these statuses: Late, On time, Late, Review, Late, On time, and a blank. With those statuses in B2:B8, this formula counts the three rows marked Late: =COUNTIF(B2:B8,"Late"). The answer is 3. You can use a similar formula for other labels by changing the criterion.
If you want to count the status selected in another cell, put the desired status in D1. With the statuses in B2:B8 and the criterion in D1, this formula counts cells equal to the value in D1: =COUNTIF(B2:B8,D1). For instance, if D1 contains On time, the result is 2.
A practical layout might have a status column and a separate visual treatment:
- Keep the actual status in a column such as B.
- Use conditional formatting to color each status consistently.
- Count the status values with
COUNTIFor, when several conditions are needed,COUNTIFS.
This approach is especially useful for reports that are refreshed or shared. The count remains tied to the data rather than to formatting that can be changed accidentally.
Using a Helper Column for Manually Colored Cells
If the colors were applied manually and there is no existing status field, add one. For example, if the colored cells are in A2:A20, enter a label such as Blue or Yellow in a new column for each row. Apply the same fill to the label cell if that helps people scan the sheet. Then count the labels rather than trying to make a formula inspect the fills.
With the color labels in C2:C20 and the label Blue used for blue-filled cells, this formula counts the blue category: =COUNTIF(C2:C20,"Blue"). The result is the number of rows labeled Blue. If a cell’s color changes, update its label as well; otherwise the count will no longer describe the displayed colors.
For a large list, make the helper column a controlled field rather than relying on free typing. A Data Validation list can reduce variations such as “Blue,” “blue,” and “Blu.” The labels can also explain what the colors mean, which is valuable for anyone who did not create the workbook.
VBA Function for Directly Applied Fill Colors
For a one-off workbook where the fill itself is the information, a small VBA user-defined function can compare a range’s direct fill colors with a sample cell. This option requires the desktop version of Excel and a macro-enabled workbook. It is intended for colors applied directly through cell formatting, not colors displayed by conditional formatting.
With the cells to inspect in A2:A20 and a sample cell C1 filled with the color to count, use this worksheet formula: =CountFillColor(A2:A20,C1). The answer is the number of cells in A2:A20 whose direct fill matches C1. The function below counts matching fills, including blank cells that have that fill.
In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste the code into the module. On a Mac, Option+F11 opens the editor on many keyboards; a Function key may also be needed, depending on keyboard settings. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm).
Function CountFillColor(dataRange As Range, sampleCell As Range) As Long
Dim cell As Range
Dim targetColor As Long
Application.Volatile
targetColor = sampleCell.Interior.Color
For Each cell In dataRange.Cells
If cell.Interior.Color = targetColor Then
CountFillColor = CountFillColor + 1
End If
Next cell
End Function
Formatting changes do not automatically trigger normal formula recalculation. After changing a fill, recalculate the workbook using Formulas > Calculate Now; on Windows, F9 also recalculates formulas. Because the function is volatile, it runs during a calculation, but that does not make the format change itself a calculation trigger.
This function uses Interior.Color, which reads a cell’s directly assigned fill. If conditional formatting supplies the visible color, the function may return a count that does not match what you see. Excel for the web cannot run VBA macros, so this method is not available there. Before running VBA code, save a copy of the workbook: macro actions cannot be undone with Ctrl+Z.
Counting Colors Created by Conditional Formatting
Conditional formatting displays a color when a rule is true—for example, when a status equals Late or a value falls below a threshold. In that situation, count the rule’s underlying condition, not the resulting color. A value may change and cause the displayed fill to change automatically, while the cell’s ordinary fill setting remains unchanged.
Suppose B2:B8 contains the statuses in the earlier example, and a conditional-formatting rule colors cells containing Late. With those statuses in B2:B8, this formula counts the same condition that causes the fill: =COUNTIF(B2:B8,"Late"). It returns 3 for the example data. If the rule is more complex—for example, a row is colored only when a status is Late and a date is overdue—count the same underlying conditions with an appropriate multi-condition formula, rather than attempting to count the displayed color.
Matching the counting rule to the formatting rule is important. If the two rules differ, the total can look plausible while still being wrong. When the conditional-formatting formula is complicated, document the rule and test the count against a few known rows.
Quick Check: Filter the Sheet by Color
If you only need to inspect a count once, filtering by color can be faster than adding VBA. Turn on filters for the data, open the filter dropdown for the colored column, and choose Filter by Color, then select the fill color. Excel hides rows that do not match, letting you review the matching records.
To get a quick visible count, select the relevant cells in the filtered results and check the status bar at the bottom of the Excel window. The status bar can show a Count for selected nonblank cells; if it is not displayed, right-click the status bar and enable Count. Be aware that this is an inspection aid, not a formula result. The count can change when the filter or selection changes, and counting nonblank cells will not include blank cells just because they have a fill.
Filter-by-color is most useful for a short task, such as checking how many reviewed entries are highlighted. It is less suitable as a report metric that must update in a dashboard or feed another calculation.
Common Problems and How to Avoid Them
- The total ignores a visible color. The fill may come from conditional formatting, while a VBA function that reads
Interior.Colorsees only the direct fill. Count the rule’s condition instead. - The VBA result does not change after recoloring. Formatting does not trigger ordinary recalculation. Run Calculate Now, and make sure the function compares the intended range and sample cell.
- Two cells look the same but do not match. Similar-looking colors can have different color values, especially if one was copied from another workbook or adjusted manually. Use a sample cell copied from the exact fill you want to count.
- Blank cells appear in the result. The example VBA function counts matching fills whether or not a cell contains a value. If blanks should not count, revise the function to test for a nonblank value as well as a matching fill.
- The formula returns an error in a shared workbook. VBA functions require desktop Excel with macros enabled. A workbook opened in Excel for the web cannot calculate this custom function there; use a helper column or a value-based count for broad compatibility.
These issues are reasons to treat color as presentation rather than data whenever possible. A clear status field is easier to audit, filter, and count than a fill that conveys meaning by itself.
Choosing the Right Approach
Choose based on whether the color is a temporary visual aid or the only record of a category:
- Use COUNTIF on a value when color represents a status or a rule based on data. This is the most reliable and portable choice.
- Add a helper column when existing manual fills need to be converted into explicit categories. It takes some setup but makes future counts transparent.
- Use VBA when direct fill color is genuinely the information you need to count and the workbook will be used in desktop Excel with macros enabled.
- Filter by color when you need a quick visual check and do not need a permanent formula-driven total.
For shared workbooks, automated reports, and files used in Excel for the web, a value-based method is generally the safest choice. It avoids macro security prompts and does not depend on whether a particular fill was applied directly or by a rule.
Frequently Asked Questions
Can COUNTIF count cells by fill color?
No. COUNTIF evaluates cell values against a criterion; it does not inspect fill formatting. Use a helper value, a filter for a quick check, or VBA for direct fills.
Can I count cells colored by conditional formatting?
Count the condition that triggers the formatting. A conditional-formatting rule may change the displayed color without changing the cell’s direct fill, so a basic fill-color VBA function is not a dependable way to count those displayed colors.
Can I count a font color instead of a fill color?
COUNTIF cannot inspect font color either. Store the category or reason for the font color in a cell and count that value, or use a suitable VBA approach if formatting itself must be inspected.
Will a VBA color-count function update as soon as I change a fill?
Usually not. A formatting change does not itself cause normal formula recalculation. Recalculate the workbook after changing fills, and remember that Excel for the web cannot run the VBA function.
What is the best way to count colored cells in a workbook used by other people?
Use a status or category column and count that value. It works without macros, makes the meaning of the color explicit, and is easier for collaborators to check and maintain.
Related
- How to Freeze Top Row and First Column in Excel
- Excel conditional formatting based on another cell?
- How to Calculate Age in Excel from Date of Birth
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel SUBSTITUTE Multiple Values: Formulas and TipsTo substitute multiple values in Excel, nest the SUBSTITUTE function for each old-to-new text pair, or use a…
- VLOOKUP in Excel: Return a Value from Another CellTo make VLOOKUP return a value from another cell, look up a key in the leftmost column of…
- Filter an Excel Table by Value: Steps, Tips, and FixesTo filter an Excel table by value in Microsoft Excel for Microsoft 365 on Windows, click the arrow…
- Excel Sum Between Dates: SUMIFS Formulas and ExamplesIf dates are in A2:A100, amounts are in B2:B100, and the start and end dates are in E2…
- Concatenate an Excel List: Join Values With DelimitersTo concatenate an Excel list into one cell, use TEXTJOIN: with the values in A2:A6, enter =TEXTJOIN(", ",TRUE,A2:A6).…
- Excel Last Day of Month: Formula and ExamplesTo return the last day of the month for a date in Excel, use =EOMONTH(A2,0), where A2 contains…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.