Forum Discussion
Deleted
Oct 12, 2017How to count and sum "Condtional formatting" cells by color in Excel 2010?
I installed few add-ins for counting "Conditional formatted" cells but all are generating an instant (one-time) numeric result by activating that command. Also, I tried a code which gives an inst...
- 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
Haziyatul Najihah Hasin
Aug 12, 2018Copper Contributor
the code doesnt work with my file..can you help me?
Jamil
Aug 13, 2018Bronze Contributor
Hi Haziyatul,
I checked your file. The range where the conditional formatting is applied and the range where the UDF uses have to be the same and they are not the same in your excel file.
CF uses $M$9:$EA$56 and UDF uses X9:Y13
so to make this work, you need to either exactly set CF for each of the vertical lines.
however, you would not need to use the UDF for this, you can simply achieve the same result by using the SUMIF or SUMPRODUCT formula. you can see the examples in my earlier posts in the same thread.
I checked your file. The range where the conditional formatting is applied and the range where the UDF uses have to be the same and they are not the same in your excel file.
CF uses $M$9:$EA$56 and UDF uses X9:Y13
so to make this work, you need to either exactly set CF for each of the vertical lines.
however, you would not need to use the UDF for this, you can simply achieve the same result by using the SUMIF or SUMPRODUCT formula. you can see the examples in my earlier posts in the same thread.
- MollyKittiSep 02, 2019Copper Contributor
I have a rather large excel file with 480 columns and 1451 rows. I currently have conditional formatting set up to find - and color - the largest value in each row. What I need to know is the count of row maximums in each column. I see that your solution only works when the count function and conditional formatting are applied to the same range. I already know how many I have per row - 1. Is there any way to count conditional formatting when the ranges don't match?
- JamilSep 04, 2019Bronze ContributorI could not understand your question.
If you are trying to count number of cells when condition is not matched, then you simply sort your data by color of "none" and then count.- MollyKittiSep 05, 2019Copper Contributor
No, I'm trying to count when the condition is met, however, my conditional formatting runs across rows. I need to count instances when conditional formatting is matched within columns. As you had previously mentioned to someone else, your tool for counting conditional formatting only works when the range and the area being counted match. My question is: is there a workaround for when the conditional formattting range and the range being summed do not match?
FYI the same condition is being tested for within each row for each row within the summed columns.
- marceloanexaApr 21, 2019Copper Contributor
Jamil Hi Jamil, I thank you for your work and effort to help.
But, it seems your code is not working here because I need to see conditional formatting changing colors at columns by the values of the cells and I need to count those color changes at rows direction, after that.
Do you think that it can be done?