VBA Count Font Colour

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hi, i found this code but it kindly lag somehow..
it keep counting and got the % show on the excel bar.
And it also included empty cell that has font colour

=CountColour(F2:BO2,$A$1)

Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double 'Update by Extendoffice Application.Volatile Dim rng As Range For Each rng In pRange1 If rng.Font.Color = pRange2.Font.Color Then CountColour = CountColour + 1 End If Next End Function
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
A change in font color does not indicate a change to a cell, so a function like this must be Volatile. That means it runs any time there is any change in the worksheet, even if the change does not affect font color. So it recalculates a lot. How many cells have this formula?

Font color is a property of a cell. It doesn't matter if the cell is empty, it still has a font color. If you want to count only non-blank cells you must change the code:

Rich (BB code):
Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
'Update by Extendoffice
Application.Volatile
Dim rng As Range
For Each rng In pRange1
    If rng.Value <> "" and rng.Font.Color = pRange2.Font.Color Then
        CountColour = CountColour + 1
    End If
Next
End Function
 
Upvote 0
A change in font color does not indicate a change to a cell, so a function like this must be Volatile. That means it runs any time there is any change in the worksheet, even if the change does not affect font color. So it recalculates a lot. How many cells have this formula?

Font color is a property of a cell. It doesn't matter if the cell is empty, it still has a font color. If you want to count only non-blank cells you must change the code:

Rich (BB code):
Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
'Update by Extendoffice
Application.Volatile
Dim rng As Range
For Each rng In pRange1
    If rng.Value <> "" and rng.Font.Color = pRange2.Font.Color Then
        CountColour = CountColour + 1
    End If
Next
End Function
more than 5000 row hehe
was thinking possible to run it when i activate it or refreshed after one mass change?? if not it keep counting haha
 
Upvote 0
You can remove Application.Volatile but then you will have to force a manual recalculation (F9) any time you want to ensure that the formula results are current.
 
Upvote 0
You can remove Application.Volatile but then you will have to force a manual recalculation (F9) any time you want to ensure that the formula results are current.
i try to remove but everytime i change it auto recalculate hehe
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top