How to Count Cells with Specific Text and Fill/Font Color in Excel (4 Ways)

In the dataset below, we have two columns: Book Name and Status. To the right, we have a separate column that will display cell count.

 Sample Data Set

Method 1 – Apply Filter and the SUBTOTAL Function 

Steps:

  • Select the cell in the column header.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • Go to the Data tab and click on Filter.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • A filter icon will appear.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • In a blank cell, enter the following formula:
=SUBTOTAL(3,B5:B11)

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • To filter by color, click on the icon.
  • Select Filter by Color.
  • Click on a specific color.
  • Press Enter.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • You will get the result as 3, as the 3 cells are formatted with the same fill color.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • To count by specific font color, choose the Filter by Font Color.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • As a result, you will see the value as 3. As 3 of them have the same font color.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel


Method 2 – Use the Find & Select Feature

Step:

  • Click on Find & select.
  • Choose Find.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • From the Format option, select Choose Format From cell.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • Select any cell where you want to count the formats.
  • Click on Find All.
  • You will get 3, as shown below.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • To count cells with specific text font, click on the cell.
  • Choose Find All to get the result.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel


Method 3 – Run a VBA Code

Steps: 

  • Press Alt  + F11 to open VBA Macro.
  • Click on Insert.
  • Choose Module.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • Enter the following VBA codes for your table range B5:B12 and look up the value in cell E5.
Sub CountCellsByFontColor()
'Declare variables
    Dim I As Long
    Dim Number As Long
    Dim Rows As Long
    Dim OutputRng As Range
    Dim TableRng, LookupFillColor As Range
    On Error Resume Next
'Set range to variables
    Set TableRng = Range("B5:B12")
    Set LookupFillColor = Range("E5")
'Set a input box for result output
    Set OutputRng = Application.InputBox("select a cell:", "ExcelDemy", Selection.Address, , , , , 8)
    If OutputRng Is Nothing Then Exit Sub
'Command to count the rows
    xRows = TableRng.Rows.Count
    Set TableRng = TableRng(1)
'Apply loop
    Number = 0
    For I = 1 To xRows
'Set condition to match the font and count the cells
        If TableRng.Offset(I - 1, 0).Interior.ColorIndex = LookupFillColor.Interior.ColorIndex Then
                Number = Number + 1
        End If
    Next
'command to output the result
    OutputRng = Number
End Sub

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • Save the program and press F5 to run it.
  • Select any cell where you want to show the result. (i.e., F5.)
  • Press Enter.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

  • The result will show as 4, as the 4 cells from the table have the same fill color format.

Apply Filter and the SUBTOTAL Function to Count Cells with Specific Text and Fill/Font Color in Excel

Notes: To count cells with both the conditions of specific text and fill/font color add the following extra condition in the VBA code.

If TableRng.Offset(I - 1, 0).Value = LookupFillColor.Value Then
Number = Number + 1
End If
  • Enter the following codes:
Sub CountCellsByFillColor()
'Declare variables
    Dim I As Long
    Dim Number As Long
    Dim Rows As Long
    Dim OutputRng As Range
    Dim TableRng, LookupFillColor As Range
    On Error Resume Next
'Set range to variables
    Set TableRng = Range("B5:B12")
    Set LookupFillColor = Range("E5")
'Set a input box for result output
    Set OutputRng = Application.InputBox("select a cell:", "ExcelDemy", Selection.Address, , , , , 8)
    If OutputRng Is Nothing Then Exit Sub
'Command to count the rows
    xRows = TableRng.Rows.Count
    Set TableRng = TableRng(1)
'Apply loop
    Number = 0
    For I = 1 To xRows
'Set condition to match the Fillcolor/Interior and count the cells
        If TableRng.Offset(I - 1, 0).Interior.ColorIndex = LookupFillColor.Interior.ColorIndex Then
         If TableRng.Offset(I - 1, 0).Value = LookupFillColor.Value Then
                Number = Number + 1
         End If
        End If
    Next
'command to output the result
    OutputRng = Number
End Sub

Run a VBA Code

  • Run the code to see the result.
  • The result shows 2, as there are 2 cells with the same text and fill color.

Run a VBA Code


Method 4 – Perform a VBA Code

Steps:

  • Open the VBA Module
  • Enter the following codes:
Sub CountCellsByFontColor()
'Declare variables
    Dim I As Long
    Dim Number As Long
    Dim Rows As Long
    Dim OutputRng As Range
    Dim TableRng, LookupFillColor As Range
    On Error Resume Next
'Set range to variables
    Set TableRng = Range("B5:B12")
    Set LookupFillColor = Range("E5")
'Set a input box for result output
    Set OutputRng = Application.InputBox("select a cell:", "ExcelDemy", Selection.Address, , , , , 8)
    If OutputRng Is Nothing Then Exit Sub
'Command to count the rows
    xRows = TableRng.Rows.Count
    Set TableRng = TableRng(1)
'Apply loop
    Number = 0
    For I = 1 To xRows
'Set condition to match the font and count the cells
        If TableRng.Offset(I - 1, 0).Font.ColorIndex = LookupFillColor.Font.ColorIndex Then
                Number = Number + 1
        End If
    Next
'command to output the result
    OutputRng = Number
End Sub

Run a VBA Code

  • Save the program and press F5 to run.
  • Select a blank cell where you want to get the result.
  • Click on OK.

Run a VBA Code

  • The result will appear as 3.

Run a VBA Code


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

Get FREE Advanced Excel Exercises with Solutions!
Bhubon Costa
Bhubon Costa

Bhubon Costa, B.Sc. in Naval Architecture & Marine Engineering from Bangladesh University of Engineering & Technology, has worked with the ExcelDemy since 2021. Currently, he has been working as a reviewer. Notably, he has written over 90 articles and led several VBA content development teams. He has a great passion for the fields of data analytics and data science. His areas of expertise include Excel VBA, Power Query, Pivot Table, Power BI, MySQL, PostgreSQL, machine learning, and Python... Read Full Bio

6 Comments
  1. I followed the instructions in this one
    Run a VBA Code to Count Cells with Specific Text and Fill Color in Excel
    but the result is not updating when i edit the range of cells and more cells match the criteria
    i want the result to change when i update the table
    i think this is supposed to be function not sub in the vba code but im not really sure
    i hope you can help me as i need the vba code and the function to run it
    thank you

    • Greetings DANA,
      To update the result after each entry you make, you just need to edit a one-line VBA code. It will update the table range each time you make a new entry in column B.

      The VBA Code:

      Set TableRng = Range(Range("B5"), Range("B5").End(xlDown))

      Now, make new entries and run the program to update the result.
      Hope this will work for you. Please give us feedback if you have any further queries.

  2. Reply
    Mr Steven J Bunting Feb 8, 2023 at 6:48 PM

    Hi, Is there a way to do this but where the fill of your cell has been previously decided by conditional formatting. I have cells which are conditionally formatted a colour, depending upon the text that is in them.

    I then want to count how many of these new cells have the colour in and have specific text.

  3. Hi, thank you for the suggestions.
    I have a question:

    Is it possible to have the range covering MULTIPLE COLUMNS to perform a VBA Code to Count Cells with Specific Text and Font Color?
    Thank you

    • Hi JOAO
      Thank you for your query.
      Yes, you can count cells with specific text and font color in a range covering Multiple Columns using VBA.
      I will show you two cases regarding how to count cells by font color of specific text.

      Case 1: Count Cells by Font Color of Specific Text
      First, let me explain the case a bit. Please have a look at the image.

      1

      Here, the font color in the specific text “The Prince of the Skies” (in E5) is Red, which is present in both B and C columns. To count the cells that have the same font color of that specific text, you should run the code given below:

      
      Sub CountCellsByFontColor()
          Dim rng As Range
          Dim lookupCell As Range
          Dim count As Long
          
          Set rng = Range("B5:C12")
          Set lookupCell = Range("E5")
          
          For Each cell In rng
              If cell.Font.Color = lookupCell.Font.Color Then
                  count = count + 1
              End If
          Next cell
          
          Range("F5") = count
      End Sub
      

      Note:
      cell.Font.Color = lookupCell.Font.Color → With this command, Excel is matching the font color of each cell (in Range B5:C12) with the font color of cell E5. If they match, the count variable will increase by 1.

      Once you run the code, Excel will show you the result.

      2

      Case 2: Count Cells with Specific Text and Font Color
      Let’s understand this case first.

      3

      Here, the reference text is “Pippo and Clara”. However, the font color is different in B6 and B8.
      Now, to count cells by specific text with color, write down the following code:

      
      Sub CountCellsByTextAndColor()
          Dim count As Long
          Dim tableRange As Range
          Dim lookupText As String
          Dim lookupColor As Range
          Dim cell As Range
          lookupText = InputBox("Specific Text: ")
          
          Set tableRange = Range("B5:C12")
          
          Set lookupColor = Range("E5")
          
          count = 0
          
          For Each cell In tableRange
              If cell.Value = lookupText And cell.Font.Color = lookupColor.Font.Color Then
                  count = count + 1
              End If
          Next cell
          Range("E5") = lookupText
          Range("F5") = count
          
      End Sub
      

      Once you run the code, you will get an input box.

      4

      You have to write the specific text that you desire. Then, on clicking OK, Excel will show the result.

      5

      Note:
      cell.Value = lookupText And cell.Font.Color = lookupColor.Font.Color → This command is checking two conditions
      cell.Value = lookupText: This condition checks if the value of the cell is equal to the value of the lookupText variable.
      cell.Font.Color = lookupColor.Font.Color: This condition checks if the font color of the cell is the same as the font color of the lookupColor cell.
      If both condition match, the count variable will increase by 1.

      Here, the count is 1. Because, for the text (“Pippo and Clara”), the font color matches only once in cell B8.
      Thank you. Have a good day!

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo