1
votes

Is it possible to use the CountIf function with advanced options: count cells containing a specific string only if the cell background is of a specific color.

I'm using the Excel formula: `=COUNTIF(page001!B:B;"id-p01"), but blocks of data on each sheet have unique strings, each block could have two different background colors: GREEN or BLUE. So what i'm asking is if i can get a function which would e.g. COUNT cells containing "id-p01" on a selected sheet, but ONLY those with a GREEN background color.

Here is an example of how the sheet looks like:

=COUNTIF(page001!B:B;"*id-p01*")

With this formula: =COUNTIF(page001!B:B;"*id-p01*") It counts id-p01 on the selected sheet in the B:B column.

Is it possible to make it count only GREEN background colored cells?

2
There are a large number of Do<something>ByColor user defined functions available with a little research.user4039065
This should serve as a starter for you - stackoverflow.com/questions/15887257/…Bharat Anand
Here is few moreDhamo

2 Answers

1
votes

This quick solution will print out on the screen the number of cells within the Range B1 to B1000 (you can modify the Range if you've more/less rows to test) that have exactly your green color.

Note that you have to use a macro to do this, it can't be achieved with a simple formula. To create a macro, press ALT + F11, then right-click on your Workbook's name and "Insert Module". Copy paste the code below and press F5 while you're still in the VBA window or use any other method to run the macro.

Sub CountWithColor()

For Each c In Range("B1:B1000")
    If c.Value Like "*id-p01*" And c.Interior.Color = RGB(226, 239, 218) Then
    compteur = compteur + 1
    End If
Next c

MsgBox (compteur)

End Sub

Let me know if this helped.

Eleove

0
votes

Count If Value And Color

Function CIVAC(Range As Range, Value As Variant, _
    Optional ColorIndex As Long = -4142, _
    Optional Compare As Integer = 1) As Long
'Title
  'Count If Value And Color
'Description
  'In a specified contiguous range, counts the number of cells both,
  'containing a specified value and having a specified Interior ColorIndex.

  Dim arrVal As Variant 'Range Array
  Dim arrClr() As Long 'ColorIndex Array
  Dim lngVal As Long 'Row Counter
  Dim iVal As Integer 'Column Counter
  Dim lngResult As Long 'Result Accumulator

  'Values
  arrVal = Range.Areas(1) 'Prevent Multiple Areas Error

  'ColorIndexes
  ReDim arrClr(LBound(arrVal) To UBound(arrVal), _
      LBound(arrVal, 2) To UBound(arrVal, 2))
  For lngVal = LBound(arrClr) To UBound(arrClr)
    For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
      arrClr(lngVal, iVal) = Range.Cells(lngVal, iVal).Interior.ColorIndex
    Next
  Next

  'Count
  For lngVal = LBound(arrClr) To UBound(arrClr)
    For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
      If Not IsError(arrVal(lngVal, iVal)) Then 'Prevent VBA Errors
        If InStr(1, arrVal(lngVal, iVal), Value, Compare) <> 0 And _
            arrClr(lngVal, iVal) = ColorIndex Then lngResult = lngResult + 1
      End If
    Next
  Next

  CIVAC = lngResult

End Function

That's nice, but what's the 'Interior ColorIndex' of the color in this cell?

Cell Interior Color Index

Function CICI(CellRange As Range) As Long
'Title
  'Cell Interior Color Index
'Description
  'Returns the Interior ColorIndex of a specified cell ('CellRange').
  'If 'CellRange' contains more than one cell, it uses the first cell.

  CICI = CellRange(1, 1).Interior.ColorIndex

End Function