Home » excel » excel – Count number of conditional formatted cells using macro

excel – Count number of conditional formatted cells using macro

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to count the number of conditional formatted cells in a row.

This function however returns the total number of cells, even those that did not pass the conditional formatting criteria:

Public Function CountColour(pRange1 As Range, pRange2 As Range) As Double
Application.Volatile
Dim rng As Range
For Each rng In pRange1    
    If rng.FormatConditions(1).Interior.Color = pRange2.FormatConditions(1).Interior.Color Then
        CountColour = CountColour + 1
    End If
Next
End Function

Any idea?

It is a school test where the number of right answers needs to be calculated.

school test

How to&Answers:

Skip trying to count the conitional formatting and count the condition:

=SUMPRODUCT(--($E$3:$G$3=$E5:$G5))

enter image description here