Excel Countif Colored: How to Count Cells by Color
In this article
Excel does not have a built-in COUNTIF function that recognizes cell fill colors or font colors directly. To achieve this, you must use a workaround such as the Filter and SUBTOTAL method, the Find and Replace tool, or a custom User Defined Function (UDF) via VBA. These methods allow you to quantify data that has been manually highlighted or categorized by color, ensuring your reporting remains accurate even when standard formulas cannot see the formatting.
What You'll Need
- A computer with Microsoft Excel installed (Windows or macOS).
- A dataset where cells are colored either manually or via Conditional Formatting.
- Basic familiarity with the Excel Ribbon and the Formula bar.
- For the advanced method: Permission to enable macros in your workbook.
Step-by-Step
Depending on your technical comfort level and whether your workbook can support macros, choose one of the following three methods.
Method 1: Using Filter and SUBTOTAL (Fastest for Manual Colors)
This is the most practical method for users who need a quick count without writing code. It uses the Filter tool to isolate colors and a specific formula to count only the visible results.
- Click anywhere inside your data range to activate the data context.
- Go to the Data tab on the Ribbon and click the Filter button. You will now see drop-down arrows in the header row of your columns.
- Click the drop-down arrow in the column containing the colored cells.
- Hover over Filter by Color and select the specific fill color you want to count from the list. Excel will now hide all rows that do not match that color.
- In an empty cell outside your filtered range (usually above or below the data), enter the formula:
=SUBTOTAL(103, A2:A100). ReplaceA2:A100with the actual range of your data. - The number
103tells Excel to use theCOUNTAfunction but to ignore any rows that have been hidden by the filter. The result is the exact count of cells with that specific color.
Method 2: Using Find and Replace (No Formulas Needed)
This method is ideal if you only need a one-time count and do not need the number to update automatically when data changes.
- Press
Ctrl + F(Windows) orCmd + F(Mac) to open the Find and Replace dialog box. - Click the Options >> button to expand the search settings.
- Click the Format... button on the right side of the dialog. If you see a drop-down arrow next to it, select Choose Format From Cell....
- Your cursor will turn into a dropper tool. Click on a cell that has the color you want to count.
- Click Find All.
- Look at the bottom-left corner of the Find and Replace window. Excel will display a message saying . This number is your total count for that color.
Method 3: Using VBA for a Custom "COUNTCOLOR" Function
If you need a permanent formula that works like a standard COUNTIF, you can create a User Defined Function (UDF). This requires the Developer tab or the Visual Basic Editor.
- Press
Alt + F11to open the Visual Basic for Applications (VBA) editor. - In the menu bar, go to Insert > Module. This opens a blank white window.
- Copy and paste the following code exactly into the module window:
Function CountColor(countRange As Range, colorCell As Range) As Long Dim cell As Range Dim colorIndex As Long colorIndex = colorCell.Interior.Color For Each cell In countRange If cell.Interior.Color = colorIndex Then CountColor = CountColor + 1 End If Next cell End Function - Close the VBA window and return to your Excel sheet.
- To use the function, click in a cell and type:
=CountColor(A2:A100, C1). In this example,A2:A100is the range you want to count, andC1is a cell that is already filled with the color you are searching for. - Press Enter. Excel will now count every cell in the range that matches the color of cell C1.
- Save your file as an Excel Macro-Enabled Workbook (.xlsm); otherwise, the code will be deleted when you close the file.
Common Mistakes
- Confusing Conditional Formatting with Manual Fill: The VBA method described above works for cells colored manually. It does not detect colors applied via Conditional Formatting rules. To count cells colored by rules, you should use the
COUNTIFSfunction based on the logic of the rule itself (e.g., if cells are red because they are < 50, use=COUNTIF(A2:A100, "<50")). - Forgetting to Save as .xlsm: Many users write the VBA code, test it, and then save the file as a standard
.xlsx. This strips all macros from the file, and the#NAME?error will appear the next time the file is opened. - Using the Wrong SUBTOTAL Index: Using
SUBTOTAL(3, ...)instead ofSUBTOTAL(103, ...)can lead to errors. The 100-series indices are specifically designed to ignore manually hidden rows as well as filtered rows. - Range Mismatches: When using the VBA function, ensuring the
colorCellreference is absolute (e.g.,$C$1) is critical if you plan to drag the formula down to count multiple different colors.
Tips
- Create a Color Key: Instead of remembering which color means what, create a small legend on the side of your sheet. Use these legend cells as the
colorCellreference in your VBA formula for a professional, dynamic dashboard. - Use Tables: Convert your data range into an official Excel Table (
Ctrl + T). This ensures that as you add new rows, yourSUBTOTALor VBA range expands automatically. - Combine with Slicers: If you use Method 1, you can add a Slicer to your table to make filtering by color even faster and more visual.
- Avoid Over-Reliance on Color: Color is a visual aid, not a data type. Whenever possible, add a helper column with a text label (e.g., "High", "Medium", "Low") and color the cells based on those labels. This makes your data searchable, sortable, and compatible with standard
COUNTIFformulas.
Troubleshooting
If your results are not appearing correctly, check the following in order:
- Check for Mixed Colors: Ensure the cells are the exact same shade. Excel treats "Light Red" and "Dark Red" as entirely different colors. If you used a custom palette, the VBA function will only count exact matches.
- Verify Macro Settings: If the VBA function returns a
#NAME?error, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure that macros are enabled or that you have clicked "Enable Content" in the yellow warning bar at the top of the sheet. - Force a Recalculation: VBA functions do not automatically trigger a recalculation when you change a cell's color (because changing a color isn't considered a "data change"). To update the count, press
F9orCtrl + Alt + F9to force Excel to recalculate all formulas. - Check for Hidden Rows: If using the
SUBTOTALmethod and the count seems too low, check if you have manually hidden rows in addition to the filter. Ensure you are using the103argument to account for all hidden cells.
Frequently Asked Questions
Can I use COUNTIF with colors in Excel Online?
No, Excel for the Web does not support VBA macros, so the custom function method will not work. You must use the Filter and SUBTOTAL method or the Find and Replace method in the desktop application.
Does the VBA method work for Conditional Formatting?
No. The Interior.Color property in VBA only detects manual fills. To count cells colored by Conditional Formatting, you must use a COUNTIF formula that mirrors the logic used in the formatting rule.
Is there a way to sum values based on color?
Yes. You can modify the VBA function to use Sum instead of Count, or use the SUBTOTAL(109, range) function after filtering by color to sum the visible values.
Will these methods work on a Mac?
Yes, all three methods work on macOS. However, the keyboard shortcuts differ (e.g., Cmd + F instead of Ctrl + F) and the VBA editor interface looks slightly different than the Windows version.
How do I count cells with NO fill color?
In the Filter method, select No Fill under the Filter by Color menu. In the VBA method, you can reference a cell that has no fill as your colorCell, though you may need to adjust the code to handle xlNone specifically.