2016-08-16 466 views
0

我有以下宏CountCellsByColor(ORIGNAL BELOW)但是我想修改它,以便通過顏色和單元格的特定文本統計單元格。使用COUNTIF來計算顏色的單元格

例如:該範圍有5個不同的名稱,所有顏色的顏色都不同。我希望宏只對與參考單元具有相同名稱和顏色的單元格進行計數。即 「弗雷德」「數yellow'cells

原配方BELOW:

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 
+0

感謝傑里米,你原來的工作意見即 - 如果indRefColor = cellCurrent.Interior.Color而cellCurrent.Value = SEARCHTEXT然後 - 我只是didnt進入它最初寫。非常感謝您的協助。真的很感激它 – d735

回答

1

應該足以改變

If indRefColor = cellCurrent.Interior.Color Then 

If (indRefColor = cellCurrent.Interior.Color) AND (cellRefColor.cells(1,1).value=cellCurrent.value) Then 
-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