Forum Discussion
How to count and sum "Condtional formatting" cells by color in Excel 2010?
- Feb 26, 2018
I came across this post being unanswered. so here is a User Defined Function in VBA
to SUM
use this UDF
Function SumConditionColorCells(CellsRange As Range, ColorRng As Range) Dim Bambo As Boolean Dim dbw As String Dim CFCELL As Range Dim CF1 As Single Dim CF2 As Double Dim CF3 As Long Bambo = False For CF1 = 1 To CellsRange.FormatConditions.Count If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then Bambo = True Exit For End If Next CF1 CF2 = 0 CF3 = 0 If Bambo = True Then For Each CFCELL In CellsRange dbw = CFCELL.FormatConditions(CF1).Formula1 dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1) dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1)) If Evaluate(dbw) = True Then CF2 = CF2 + CFCELL.Value CF3 = CF3 + 1 Next CFCELL Else SumConditionColorCells = "NO-COLOR" Exit Function End If SumConditionColorCells = CF2 End Function
if you want to Count instead of SUM then use the below UDF
Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range) Dim Bambo As Boolean Dim dbw As String Dim CFCELL As Range Dim CF1 As Single Dim CF2 As Double Dim CF3 As Long Bambo = False For CF1 = 1 To CellsRange.FormatConditions.Count If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then Bambo = True Exit For End If Next CF1 CF2 = 0 CF3 = 0 If Bambo = True Then For Each CFCELL In CellsRange dbw = CFCELL.FormatConditions(CF1).Formula1 dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1) dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1)) If Evaluate(dbw) = True Then CF2 = CF2 + 1 CF3 = CF3 + 1 Next CFCELL Else COUNTConditionColorCells = "NO-COLOR" Exit Function End If COUNTConditionColorCells = CF2 End Function
these solutions were provided to the similar question asked by other Excel users and worked for them.
For more detail here is the link for that answer
Hi Julie,
I checked your file.
it was not working because A) you need to enable macros in order to be able to use the UDF.
B) the sample file you upload, you had only cells for criteria colored, but there wasn't any conditional formatting set in your workbook, that's why It did not work.
I have recorded a video that shows how you can set up your conditional formatting.
please download the attached video and watch it. also i have attached your workbook with conditional formatting set in it.
Hello Jamil !
I've tried to read your answers and I have managed to install the UDF for counting condition colored cells, but at the most I am only managing the formula to say "NO-COLOR" (sum is not relevant but I've installed it too). I have looked in your example files and the video tutorial you made, but I am still not managing to get it to work, macros should be enabled. I need the sheet to count the amount of red/green cells both horizontally and vertically. The cells are formatted so that when a number within range is added to the cell it turns green, but if a number out of range is added or if the cell is empty it goes red - that part is already done (ranges are written above the cells in blue). Now I just need the color counting in order to calculate what passed and what failed. I have attached a copy of my excel document, and I would be really grateful if you had the time to look through it and check what's missing.
Kind regards, Madeleine Larsson
(English is not my native language, so I apologize if any words are improperly used)
- JamilNov 24, 2020Bronze ContributorThe formula would look like this =AND(C3>=0,5,C3<=4)
- ThanathoziaNov 24, 2020Copper ContributorHi! Okay, I've only ever been taught (both in school and at work) to use conditional formatting in the way of choosing between pre-existing settings (new rule - choose a type of rule - format cells with...). But how would a formula look for, for example, the first cell having a range between 0,5-4? In range should be light green, blow/above range + empty cell + cell containing "-" should be light red.
- JamilNov 24, 2020Bronze Contributor
I checked your file. the conditional formatting you set is not using a formula that is why it is not working.