2016-08-16 6 views
0

У меня есть следующий макрос CountCellsByColor (ORIGNAL BELOW), однако я хочу изменить его так, чтобы он подсчитывал ячейки по цвету и конкретному тексту ячейки.Подсчет клеток по цвету с COUNTIF

например: В ассортименте имеется 5 разных наименований, все цвет которых отличается от другого. Я хочу, чтобы макрос учитывал только ячейки с тем же именем и цветом, что и опорная ячейка. то есть количество "Фред" «yellow'cells

ОРИГИНАЛ ФОРМУЛА НИЖЕ:

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 Тогда - я как раз не вводить его сначала напишите. Большое вам спасибо за помощь. Действительно ценю это – 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 

Работа Sample

enter image description here