Microsoft Excel is one of the most powerful spreadsheet tools used for data management, analysis, budgeting, and organization. Many users rely on cell colors to categorize important information, such as marking completed tasks, highlighting deadlines, organizing expenses, or grouping inventory items. While Excel offers several built-in formulas like COUNTIF and SUMIF, it does not provide a direct way to count cells based on their fill color or font color. This limitation can be frustrating for users who work heavily with color-coded data.
Fortunately, Visual Basic for Applications (VBA) offers an efficient solution. By using custom VBA code, you can create functions that count cells based on their background color or text color. This method is highly useful for automating tasks and improving productivity. In this guide, you will learn how to enable VBA in Excel, insert custom code, and accurately count colored cells with easy step-by-step instructions.
What Does Counting Colored Cells Mean in Excel?
Counting colored cells means calculating how many cells in a selected range share the same fill color or font color. For example, if you use green cells for completed projects and red cells for pending work, counting these colors helps you quickly analyze progress.
Excel’s standard formulas do not recognize color as a counting criterion because formatting is considered visual data rather than numerical or text data. VBA solves this issue by reading the cell’s color properties directly and returning the count.
This can be useful for:
- Attendance sheets
- Expense tracking
- Project management
- Sales reports
- Inventory systems
- Academic grading systems
Prerequisites Before Using VBA in Excel
Before starting, ensure you have the following:
- Microsoft Excel installed
- A workbook where you want to count colored cells
- Basic knowledge of Excel navigation
- Developer tab enabled
- Workbook saved as Macro-Enabled Workbook (.xlsm)
How To Enable the Developer Tab in Excel
The Developer tab provides access to VBA tools.
Steps to enable Developer tab:
- Open Microsoft Excel
- Click File
- Select Options
- Choose Customize Ribbon
- Under Main Tabs, check Developer
- Click OK
You will now see the Developer tab in Excel’s ribbon menu.
How To Open VBA Editor
To insert VBA code:
- Open your Excel workbook
- Press Alt + F11
- In the VBA editor, click Insert
- Select Module
A blank code window will appear where you can paste your VBA code.
VBA Code To Count Cells By Background Color
Use the following code to count cells based on fill color:
Function CountColoredCells(countRange As Range, colorCell As Range) As Long
Dim dataCell As Range
Dim colorCount As Long
colorCount = 0
For Each dataCell In countRange
If dataCell.Interior.Color = colorCell.Interior.Color Then
colorCount = colorCount + 1
End If
Next dataCell
CountColoredCells = colorCount
End Function
How This VBA Code Works
This custom function compares each cell in the selected range to a reference cell’s background color.
Parameters:
- countRange = Cells you want to analyze
- colorCell = Sample colored cell for comparison
Example:
=CountColoredCells(A1:A20,C1)
If cell C1 is filled with yellow, Excel counts how many cells in A1:A20 are also yellow.
VBA Code To Count Cells By Font Color
If you want to count cells based on text color instead of fill color, use this code:
Function CountFontColorCells(countRange As Range, colorCell As Range) As Long
Dim dataCell As Range
Dim colorCount As Long
colorCount = 0
For Each dataCell In countRange
If dataCell.Font.Color = colorCell.Font.Color Then
colorCount = colorCount + 1
End If
Next dataCell
CountFontColorCells = colorCount
End Function
How To Use Font Color Counting Formula
Example:
=CountFontColorCells(B1:B30,D1)
This formula counts how many cells in B1:B30 have the same font color as D1.
Saving Your Workbook Properly
Since VBA macros are involved, save your workbook as:
Excel Macro-Enabled Workbook (*.xlsm)
Steps:
- Click File
- Select Save As
- Choose location
- Select .xlsm
- Click Save
Benefits of Using VBA To Count Colored Cells
1. Automation
No need to manually count highlighted cells.
2. Improved Accuracy
Reduces human errors.
3. Time Saving
Ideal for large spreadsheets.
4. Flexibility
Can count both fill and text colors.
5. Customization
Can be modified for advanced needs.
Common Issues and Troubleshooting
Macros Disabled
If the formula does not work:
- Go to File > Options > Trust Center
- Click Trust Center Settings
- Enable Macros
Workbook Not Saved as .xlsm
Standard .xlsx files do not support VBA.
Incorrect Color Matching
Even slight color shade differences can affect results.
Formula Not Updating
Press F9 to recalculate workbook.
Does VBA Work With Conditional Formatting?
Standard VBA code usually reads manually applied colors, not conditional formatting colors. If your spreadsheet uses conditional formatting, advanced VBA may be required.
Alternative Methods Without VBA
Filter by Color
- Select data
- Click Filter
- Choose Filter by Color
GET.CELL Function
An advanced Excel named formula method.
Manual Sorting
Sort by color and count visible rows.
Best Practices for Using VBA in Excel
- Always back up important files
- Only enable macros from trusted sources
- Test VBA code on sample sheets first
- Keep code documented for future editing
- Use consistent color schemes
Frequently Asked Questions
Can I count colored cells without VBA?
Yes, but methods are limited and less efficient. VBA is usually the best option.
Is VBA safe to use?
Yes, if the code comes from trusted sources.
Why is my formula returning zero?
Macros may be disabled, workbook format may be incorrect, or colors may not match.
Can I count multiple colors?
Yes, by using different reference cells for each color.
Final Thoughts
Counting colored cells in Excel can greatly improve the efficiency of managing color-coded data, especially for large spreadsheets used in business, education, finance, or project management. Although Excel does not provide a built-in formula for counting based on cell color, VBA offers a powerful and flexible workaround. By creating custom functions, users can automate color counting for both background and font colors with ease.
Whether you are tracking completed tasks, monitoring inventory categories, or organizing reports, VBA can save significant time and reduce manual errors. Once set up, these formulas become reusable tools for future projects. By following this guide carefully, even beginners can successfully implement VBA code and unlock advanced Excel functionality for better productivity and data analysis.


