COUNTIF function but with conditional formatted cells

%3CLINGO-SUB%20id%3D%22lingo-sub-1701016%22%20slang%3D%22en-US%22%3ECOUNTIF%20function%20but%20with%20conditional%20formatted%20cells%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1701016%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20spreadsheet%20that%20uses%20conditional%20formatting%20via%20a%20drop%20down%20list%20that%20the%20cell%20changes%20colour%20depending%20on%20result.%20I%20want%20to%20be%20able%20to%20count%20the%20number%20of%20cells%20that%20are%20certain%20colours.%20I%20have%20watch%20tutorials%20for%20Get.cell%20but%20that%20can't%20work%20with%20condition%20formatting.%20I've%20also%20looked%20at%20setting%20up%20VBA%20but%20can't%20seem%20to%20get%20that%20to%20work%20either%20wasn't%20sure%20if%20this%20is%20because%20of%20the%20formatting.%3C%2FP%3E%3CP%3EPlease%20help%20is%20there%20a%20solution%20to%20this%3F%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1701016%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hi,

 

I have a spreadsheet that uses conditional formatting via a drop down list that the cell changes colour depending on result. I want to be able to count the number of cells that are certain colours. I have watch tutorials for Get.cell but that can't work with condition formatting. I've also looked at setting up VBA but can't seem to get that to work either wasn't sure if this is because of the formatting.

Please help is there a solution to this?

Thanks,

1 Reply
Highlighted

@North_Yorks 


Below is VBA to count conditionally formatted colors by leveraging AutoFilter to select by color and DisplayFormat.Interior.Color to get a cell's displayed color created by any means including conditional formats

 

Function CountColour(ByRef oRange As Range, ByRef oSample As Range) As Long

' This function leverages AutoFilter's ability to select cells by color

' oRange is the cells (arranged in a column) to search
' oSample is a cell that is displaying the color to search for
' Alternatively, we can use lColor as a parameter instead of oSample for those
' situations when we do not have a sample to work with. See below on how to set lColor

Dim lColor As Long

' Displayed color whether from conditional formatting or not
lColor = oSample.DisplayFormat.Interior.Color

' Excel applies AutoFilter to the first non-blank cell; thus, if cells above are
' blank, they will remain visible. We don't want that. Since we are counting conditionally
' formatted cells, we should remove non-conditionally formatted cells from oRange
Set oRange = oRange.SpecialCells(xlCellTypeAllFormatConditions)

' Apply AutoFilter
oRange.AutoFilter Field:=1, Criteria1:=lColor, Operator:=xlFilterCellColor

' Count visible cells
CountColour = oRange.SpecialCells(xlCellTypeVisible).Cells.Count

' The AutoFilter cells is always visible but may not be the color we want
If oRange.Cells(1).DisplayFormat.Interior.Color <> lColor Then CountColour = CountColour - 1

' Return display to normal
oRange.AutoFilter

End Function