How to Count Colored Cells in Excel with VBA (4 Methods)

The sample dataset contains students’ marks in two subjects. We will fill with green or yellow to highlight marks greater than 80 or less than 50 respectively.

How to Count Colored Cells in Excel with VBA


Method 1 – Apply VBA Code to Count Cells by Fill Color

 

Steps:

  • Press Alt + F11 to open the VBA window.
  • Select Insert then Module.

Apply VBA to Count Cells by Fill Color In Excel

  • Enter the following codes in the module.
Function CountCellBy_FillColor(CellRange As Range, CellColor As Range)
Dim FillColor As Integer
Dim FillTotal As Integer
FillColor = CellColor.Interior.ColorIndex
Set rCell = CellRange
For Each rCell In CellRange
If rCell.Interior.ColorIndex = FillColor Then
FillTotal = FillTotal + 1
End If
Next rCell
CountCellBy_FillColor = FillTotal
End Function

 

Apply VBA to Count Cells by Fill Color In Excel

  • To count the green cells, place the cursor beside Cell F5.
  • Insert the following formula in Cell G5.
=CountCellBy_FillColor($C$5:$D$11,F5)
  • Hit the Enter button and you will get the green cells count.


Method 2 – Run a VBA Code to Count Cells by Font Color in Excel

 

Use Excel VBA to Count Cells by Font Color

Steps:

  • Press Alt + F11 to open the VBA window.
  • Select Insert then Module.
  • Enter the following codes in the module.
Function CountCellsBy_FontColor(cell_range As Range, CellFont_color As Range) As Long
Dim FontColor As Long
Dim CurrentRange As Range
Dim FontRes As Long
Application.Volatile
FontRes = 0
FontColor = CellFont_color.Cells(1, 1).Font.Color
For Each CurrentRange In cell_range
If FontColor = CurrentRange.Font.Color Then
FontRes = FontRes + 1
End If
Next CurrentRange
CountCellsBy_FontColor = FontRes
End Function
  • Return to the worksheet.

Use Excel VBA to Count Cells by Font Color

  • To count the red cells, insert the following formula in cell G5.
=CountCellsBy_FontColor($C$5:$D$11,F5)
  • Hit Enter.

 


Method 3 – Use VBA to Count Colored Cells by Row in Excel

In this dataset we have added another column and we need to count the number of cells of the same color by row.

Use VBA to Count Colored Cells by Row in Excel

Steps:

  • Press Alt + F11 to open the VBA window.
  • Select Insert then Module.
  • Paste the following code in the module.
Function Colorby_Row(rowColor1 As Range, rowColor2 As Range, rowColor3 As Range) As String
Dim rowResult As String
Dim rowCounter As Integer
mColor1 = rowColor1.Interior.Color
mColor2 = rowColor2.Interior.Color
mColor3 = rowColor3.Interior.Color
green = RGB(0, 200, 0)
rowCounter = 0
If mColor1 = green Then
rowCounter = rowCounter + 1
End If
If mColor2 = green Then
rowCounter = rowCounter + 1
End If
If mColor3 = green Then
rowCounter = rowCounter + 1
End If
If rowCounter >= 2 Then
rowResult = 1
Else
rowResult = 0
End If
Colorby_Row = rowResult
End Function
  • Go back to the worksheet.

Use VBA to Count Colored Cells by Row in Excel

  • Insert the following formula in Cell F5 and press the Enter button.
=Colorby_Row(C5,D5,E5)

Here is the result.


Method 4 – Embed VBA to Count and Sum Cells by Conditional Formatting Color

Conditional formatting has been applied to the dataset.

Embed VBA to Count and Sum Cells by Conditional Formatting Color

Steps:

  • Select the data range.

Embed VBA to Count and Sum Cells by Conditional Formatting Color

  • Press Alt + F11 to open the VBA window.
  • Select Insert then Module.
  • Enter the following code in the module.
Sub SumNCountByConditionalFormattingColor()
Dim mitRefColor As Long
Dim SampleColor As Range
Dim mitRes As Long
Dim mitSum
Dim CountCells As Long
Dim mitCurrCell As Long
On Error Resume Next
mitRes = 0
mitSum = 0
CountCells = Selection.CountLarge
Set SampleColor = Application.InputBox( _
"Choose sample color:", "Select a cell which has sample color", _
Application.Selection.Address, Type:=8)
If Not (SampleColor Is Nothing) Then
mitRefColor = SampleColor.Cells(1, 1).DisplayFormat.Interior.Color
For mitCurrCell = 1 To (CountCells)
If mitRefColor = Selection(mitCurrCell).DisplayFormat.Interior.Color Then
mitRes = mitRes + 1
mitSum = WorksheetFunction.Sum(Selection(mitCurrCell), mitSum)
End If
Next
MsgBox "Cell Count= " & mitRes & vbCrLf & "Sum of Colored Cells= " & mitSum & vbCrLf & vbCrLf & _
"Color Code= " & Left("000000", 6 - Len(Hex(mitRefColor))) & _
Hex(mitRefColor) & vbCrLf, , "Count and Sum Cells by Conditional Formatting Color"
End If
End Sub
  • Select the Run icon to run the code. It will open an input box to select the sample color from the selected range.

  • Select a cell with the preferred color, in this example Cell C6 has been selected.
  • Hit the Enter button.

A notification box showing the cells count and sum along with the color code is returned.


Download Practice Workbook

 


<< Go Back to Colored Cells | Count Cells | Formula List | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Md. Sourov Hossain Mithun
Md. Sourov Hossain Mithun

Md. Sourov Hossain Mithun, an Excel and VBA Content Developer at Softeko's ExcelDemy project, joined in October 2021. Holding a Naval Architecture & Marine Engineering degree from BUET, Mithun showcases expertise during his two-year tenure. With over 200 published articles on Excel topics, he earned a promotion to Team Leader, excelling in leading diverse teams. Mithun's passion extends to Advanced Excel, Excel VBA, Data Analysis, and Python programming, contributing significantly to the innovative and dynamic environment of ExcelDemy... Read Full Bio

2 Comments
  1. hi Mr. Md.sourav my name is galwin and i live in mumbai i got this job as document controller i face too much problems maintaing logs and to count each color according to their status can you help me with this count color. is there any formula where i can use below the whole table of colors ..like in end. the total count ..and will the formula have to be updated after every change of colors according to the upgrade in the graphs ..can there be once i added the formula it automatically updates the result or count

    • Hello Galwin,

      Congratulations on your new job as a Document Controller! I understand how time-consuming it can be to count colored cells manually. Fortunately, Excel offers a few ways to automate this task.

      If you want the count to update automatically whenever cell colors change, I recommend using VBA (Visual Basic for Applications) since regular Excel formulas don’t dynamically update based on color changes. Here’s a simple VBA solution:

      1. Press Alt + F11 to open the VBA editor.
      2. Go to Insert > Module to add a new module.
      Paste this code:

      
      Function CountColoredCells(rng As Range, colorCell As Range) As Long
          Dim count As Long
          Application.Volatile
          For Each cell In rng
              If cell.Interior.Color = colorCell.Interior.Color Then count = count + 1
          Next cell
          CountColoredCells = count
      End Function

      Press Ctrl + S to save the macro and close the editor.
      In your Excel sheet, use the formula like this:
      =CountColoredCells(A1:D10, F1)
      Replace A1:D10 with your data range and F1 with a cell that has the color you want to count.

      Automatic Update
      To make sure the count updates when colors change:

      Press Alt + F11, go to ThisWorkbook in the editor, and paste this code:

      
      Private Sub Worksheet_Change(ByVal Target As Range)
          Application.Calculate
      End Sub

      This ensures the function recalculates every time you update your worksheet.

      Regards
      ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo