2016-08-16 3 views
0

私は以下のマクロCountCellsByColor(ORIGNAL BELOW)を持っていますが、色とセルの特定のテキストによってセルを数えるように修正したいと思います。COUNTIFで色でセルをカウントする

例:範囲には5つの異なる名前があり、すべて異なる色に着色されています。私は参照セルと同じ名前と色でセルを数えさせるマクロにします。すなわち "フレッド"「yellow'cellsの番号の下

ORIGINAL FORMULA:それは

If (indRefColor = cellCurrent.Interior.Color) AND (cellRefColor.cells(1,1).value=cellCurrent.value) Then 
+0

おかげでジェレミー、あなたのオリジナルのコメントは すなわちを働いた - indRefColor = cellCurrent.Interior.ColorそしてcellCurrent.Value =検索テキスト続いた場合 - 私はちょうどdidntはそれを入力してください最初に書き込む。ご協力いただきありがとうございます。心から感謝する – d735

答えて

1

は、あなたが探している願っていますこの。

Function CountCellsByColor(rData As Range, cellRefColor As Range, searchtext As String) As Long 
    Dim indRefColor As Long 
    Dim cellCurrent As Range 
    Dim cntRes As Long 
    Application.Volatile 
    cntRes = 0 
    indRefColor = cellRefColor.Cells(1, 1).Interior.Color 
    For Each cellCurrent In rData 
     If indRefColor = cellCurrent.Interior.Color And cellCurrent.Value = searchtext Then 
      cntRes = cntRes + 1 
     End If 
    Next cellCurrent 
    CountCellsByColor = cntRes 
End Function 

ワーキングサンプル

enter image description here

-1

If indRefColor = cellCurrent.Interior.Color Then 

を変更するには十分なはず

Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long 
    Dim indRefColor As Long 
    Dim cellCurrent As Range 
    Dim cntRes As Long 

    Application.Volatile 
    cntRes = 0 
    indRefColor = cellRefColor.Cells(1, 1).Interior.Color 
    For Each cellCurrent In rData 
     If indRefColor = cellCurrent.Interior.Color Then 
      cntRes = cntRes + 1 
     End If 
    Next cellCurrent 

    CountCellsByColor = cntRes 
End Function 
関連する問題