0

请需要您的帮助来计算指定不同颜色的单元格值。

在一张纸上,一些单元格用红色填充,一些单元格用蓝色填充,一些单元格用绿色填充。输出应分别为红色细胞计数、蓝色细胞计数和绿色细胞计数。

这是我尝试过的:

Function CountByColor(InputRange As Range, ColorRange As Range) As Long

    Dim cl As Range
    TempCount As Long
    ColorIndex As Integer

    ColorIndex = ColorRange.Cells(1, 1).Interior.ColorIndex TempCount = 0
    For Each cl In InputRange.Cells
        If cl.Interior.ColorIndex = ColorIndex
        Then
        TempCount = TempCount + 1
        End If
    Next cl
    Set cl = Nothing CountByColor = TempCount
End Function
4

1 回答 1

2

您的功能按预期工作:-

Function CountByColor(InputRange As Range, ColorRange As Range) As Long
    Dim cl As Range, TempCount As Long, ColorIndex As Integer
    ColorIndex = ColorRange.Cells(1, 1).Interior.ColorIndex
    TempCount = 0
    For Each cl In InputRange.Cells
        If cl.Interior.ColorIndex = ColorIndex Then
            TempCount = TempCount + 1
        End If
    Next cl
    Set cl = Nothing
    CountByColor = TempCount
End Function

公式:

=CountByColor(A:A,A2)

这里A2 Cell充满Green绿色指数是14

结果:

对于我的工作表,我得到的结果为

3

基本上你需要执行这个公式三次才能得到三个结果

=CountByColor(A:A,A2)   // A2 filled with Green
=CountByColor(A:A,A6)   // A6 filled with Red
=CountByColor(A:A,A9)   // A9 filled with Blue
于 2012-05-22T14:16:15.560 回答