Share via

Function to Count Cells with a certain background color

Anonymous
2018-04-10T18:08:52+00:00

Is there a way to count how many cells in a range have a specific background color? I tried the macro below:

Function CountColorIf(rSample As Range, rArea As Range) As Long
    Dim rAreaCell As Range
    Dim lMatchColor As Long
    Dim lCounter As Long

    lMatchColor = rSample.Interior.Color
    For Each rAreaCell In rArea
        If rAreaCell.Interior.Color = lMatchColor Then
            lCounter = lCounter + 1
        End If
    Next rAreaCell
    CountColorIf = lCounter
End Function

 and using =CountColorIF(Reference cell, Range)

But when I enter the formula into excel, a value is not returned. The cell just shows the full formula still. Any help?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2018-04-10T18:11:07+00:00

If the cell shows the formula the cell is formatted as text. Change the formatting to General and re-enter the formula.

Andreas.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful