0

指定された異なる色でセルの値をカウントするには、あなたの助けが必要です.

1 つのシートで、いくつかのセルが赤色で塗りつぶされ、いくつかのセルが青色で塗りつぶされ、いくつかのセルが緑色で塗りつぶされます。出力は、赤色の細胞数、青色の細胞数、緑色の細胞数を別々に表示する必要があります。

これは私が試したことです:

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

基本的に、3 つの結果を得るには、この式を 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 に答える