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.
Method 1 – Apply Filter and the SUBTOTAL Function
Steps:
- Select the cell in the column header.
- Go to the Data tab and click on Filter.
- A filter icon will appear.
- In a blank cell, enter the following formula:
=SUBTOTAL(3,B5:B11)
- To filter by color, click on the icon.
- Select Filter by Color.
- Click on a specific color.
- Press Enter.
- You will get the result as 3, as the 3 cells are formatted with the same fill color.
- To count by specific font color, choose the Filter by Font Color.
- As a result, you will see the value as 3. As 3 of them have the same font color.
Method 2 – Use the Find & Select Feature
Step:
- Click on Find & select.
- Choose Find.
- From the Format option, select Choose Format From cell.
- Select any cell where you want to count the formats.
- Click on Find All.
- You will get 3, as shown below.
- To count cells with specific text font, click on the cell.
- Choose Find All to get the result.
Method 3 – Run a VBA Code
Steps:
- Press Alt + F11 to open VBA Macro.
- Click on Insert.
- Choose Module.
- 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
- Save the program and press F5 to run it.
- Select any cell where you want to show the result. (i.e., F5.)
- Press Enter.
- The result will show as 4, as the 4 cells from the table have the same fill color format.
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 the code to see the result.
- The result shows 2, as there are 2 cells with the same text and fill color.
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
- Save the program and press F5 to run.
- Select a blank cell where you want to get the result.
- Click on OK.
- The result will appear as 3.
<< Go Back to Colored Cells | Count Cells | Formula List | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
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:
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.
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.
Hi MR STEVEN J BUNTING,
Greetings. Thanks for commenting. Yes, you can count cells by color with conditional formatting in Excel. To do this, you can follow three procedures: filter feature, table feature, and sort feature. Please follow the following articles if you would like to know more details.
Count Cells by Color with Conditional Formatting in Excel (3 Methods)
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.
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:
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.
Case 2: Count Cells with Specific Text and Font Color
Let’s understand this case first.
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:
Once you run the code, you will get an input box.
You have to write the specific text that you desire. Then, on clicking OK, Excel will show the result.
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!