Excel has no built-in COUNTIF formula that counts cells by fill color. For a quick count, select the cells and use Excel’s status bar; for a reusable worksheet result, use a helper column or a VBA function, depending on whether the highlight is a manually applied fill or comes from conditional formatting.
Why COUNTIF Cannot Count Cell Color
COUNTIF evaluates cell values against a condition, such as whether a number is greater than 10 or text equals “Complete.” A cell’s fill color is formatting, not a value or ordinary worksheet condition, so a formula such as =COUNTIF(A2:A20,"yellow") does not count yellow cells. It looks for the text “yellow” in the cells.
This distinction matters because color can be applied in different ways. Someone may choose a fill color from the Home tab, copy formatting from another cell, or set a conditional formatting rule that changes the displayed fill when a value meets a condition. Excel formulas do not normally inspect a cell’s displayed fill color.
Fastest Method: Count a Manual Fill with the Status Bar
If you only need a one-time count of cells filled manually, select the range. The status bar at the bottom of the Excel window may show a count of selected cells, but its default options do not include a count by fill color. Instead, filter the range by color and read the resulting count.
- Select the data range, including its header row, and choose Home > Sort & Filter > Filter.
- Open the filter arrow for the column whose cells have the fill color.
- Choose Filter by Color, then select the fill color to count.
- Select the visible cells in the relevant data column. The status bar’s Count value reports the number of nonblank selected cells. If blank highlighted cells matter, this count will omit them.
Filtering hides rows that do not match; it does not delete them. Clear the filter when finished. This approach is convenient for a quick check, but it is not a formula result that updates in a worksheet cell.
Best Formula-Based Option: Count the Rule or Meaning
When color represents a category or condition, count the underlying data rather than the formatting. This is usually the most reliable and maintainable solution: store the category or status in a cell, then use COUNTIF on that value. The result updates when the data changes and does not depend on someone remembering to apply the right fill.
For example, with statuses in A2:A20 and some cells shaded green to indicate “Complete,” enter =COUNTIF(A2:A20,"Complete") in any empty cell. If five entries say Complete, the formula returns 5. You can then use conditional formatting to color those cells based on the status, keeping the count and appearance tied to the same data.
If the highlight represents a numeric rule, count that rule directly. With scores in B2:B20, the formula =COUNTIF(B2:B20,">=80") counts values of 80 or more. It returns 6 if six scores meet that threshold, regardless of whether those cells are currently colored.
Count Cells by Manual Fill Color with VBA
If the fill color itself is the information and you need a count in a worksheet cell, VBA can inspect the cell’s interior formatting. Excel does not provide a standard worksheet COUNTIF-by-color function. The following user-defined function counts cells with the same manually applied fill as a reference cell.
With the cells to count in A2:A20 and a sample cell with the desired fill in D1, enter =CountFillColor(A2:A20,D1) in a worksheet cell. If four cells in A2:A20 have the same fill as D1, the result is 4. The sample cell can be outside the counted range.
To add the function in Excel for Microsoft 365 on Windows, press Alt+F11, choose Insert > Module, and paste this code into the module. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). On Mac, open the Visual Basic Editor using Tools > Macro > Visual Basic Editor, then insert a module in the same way.
Function CountFillColor(countRange As Range, sampleCell As Range) As Long
Dim cell As Range
Dim total As Long
' Count cells with the same manually applied fill as the sample.
For Each cell In countRange.Cells
If cell.Interior.Color = sampleCell.Interior.Color Then
total = total + 1
End If
Next cell
CountFillColor = total
End Function
Save a copy of the workbook before running or relying on a macro. Macros cannot be undone with Ctrl+Z. Keep in mind that a VBA function may not recalculate just because you changed a fill color: formatting changes generally do not trigger normal worksheet recalculation. After changing fills, force a calculation with F9 on Windows or Fn+F9 on many Mac keyboards, or re-enter the formula. Macro security settings may also prevent the function from running.
Conditional Formatting Needs a Different Approach
The VBA function above checks Interior.Color, which represents a cell’s directly applied fill. It does not reliably count the color that appears because a conditional formatting rule is active. For conditional formatting, count the rule’s underlying condition instead.
For example, if the rule colors scores in B2:B20 green when they are at least 80, use =COUNTIF(B2:B20,">=80") to count the cells meeting that rule. If the rule depends on several conditions, use COUNTIFS with matching criteria. With scores in B2:B20 and statuses in A2:A20, this formula counts rows marked Complete with a score of at least 80: =COUNTIFS(A2:A20,"Complete",B2:B20,">=80"). It returns 3 if three rows meet both conditions.
COUNTIFS is preferable to counting a displayed color because it expresses the actual logic. If the conditional formatting rule changes, update the formula’s criteria to match it. For complicated rules, compare the formula with the rule under Home > Conditional Formatting > Manage Rules.
Common Problems and Practical Choices
- Similar-looking colors: VBA compares color values, not what a person perceives as “close enough.” Two similar shades can have different color codes and will not match.
- Blank colored cells: A filter-and-status-bar count of nonblank cells will miss blank cells. The VBA function counts matching fills whether or not the cells contain values.
- Changing a fill does not update the count: A formatting change may not trigger formula recalculation. Recalculate or use a data-based formula instead.
- Copied or theme colors: A fill copied from another workbook or selected from a theme may not match a separately chosen shade exactly.
- Sharing the workbook: VBA functions require macros to be enabled and are not a dependable option in Excel for the web, where VBA cannot run. Prefer a value-based COUNTIF or COUNTIFS formula for shared workbooks.
In short, use a filter for a quick manual check, COUNTIF or COUNTIFS when color represents a value or rule, and VBA only when you specifically need to inspect manually applied fills in desktop Excel.
Frequently Asked Questions
Can COUNTIF count cells by background color?
No. COUNTIF checks cell contents against criteria, not fill color. Count the underlying value or condition, or use VBA for manually applied fills.
Can I count cells highlighted by conditional formatting?
Count the condition that triggers the rule with COUNTIF or COUNTIFS. A basic VBA function that checks Interior.Color does not reliably return the displayed conditional-formatting color.
Does the VBA formula update when I change a cell’s color?
Not necessarily. Excel may not recalculate a worksheet function after a formatting-only change. Press F9 on Windows or Fn+F9 on many Mac keyboards, or re-enter the formula.
Can I use this color-count formula in Excel for the web?
Excel for the web does not run VBA macros. Use a value-based formula such as COUNTIF or COUNTIFS, or open the workbook in desktop Excel to use the VBA function.
What is the safest way to count highlighted cells in a shared workbook?
Put the meaning behind the highlight in a status or category column, then count that data with COUNTIF or COUNTIFS. This avoids macro restrictions and makes the result easier to audit.
Related
- Highlighted Named Range in Excel: How to See It
- Format Highlighted Cells in Excel: Quick Steps
- How to Delete Highlighted Cells in Excel
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel Filter Not Equal to Text? How to Exclude ValuesTo filter out a particular text value in Excel for Microsoft 365 on Windows, select a cell in…
- Excel: Assign Unique Numbers to Duplicate Values in RowsYes. In Excel for Microsoft 365 on Windows, you can number each repeated value by its occurrence with…
- XLOOKUP Excel Conditional Search: Formulas and ExamplesTo use XLOOKUP for a conditional search in Excel, put the condition in its lookup array; for multiple…
- Excel COUNTIF Contains Partial Text: 3 WaysTo count cells that contain part of text in Excel, use =COUNTIF(range,"*text*") — the asterisks are wildcards that…
- Excel Date Minus Date in Years? DATEDIF ExplainedThe direct answer: use =DATEDIF(start_date, end_date, "Y") to get whole years between two dates in Excel. For example,…
- Excel IF OR Formula: Combine Multiple Conditions EasilyTo use Excel’s IF with OR, put OR in IF’s logical-test position: Excel checks whether at least one…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.