Forum Discussion
sum by color when colors are set by conditional formatting
- Jan 19, 2017
Hi matt nipper,
since you mentioned that you "have done an exhaustive search online" i have come up with a solution for you, although it has two limitations A) it will only work, if your rules of conditional formatting is created using conditional formatting rules with formula aka (use a formula to determine which cells to format) and B) the UDF will only work if sum range is more than one cell another word, it will not sum a single cell, as well as the conditional formatted range is more than one cell.
the example file, you can download it from here. I could not upload it here, as it is a Excel Macro-enabled Workbook that contains the 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
in the attached example file you can see that from A3:G16 cells are formatted using Conditional Formatting.
User Defined Function (UDF) is placed in cells J2 & J3 and cells I2 and I3 are the criteria color used as reference inside the UDF in J2 and J3.
I hope this helps you.
Edit: uploaded file and updated the code to the correct one.
Yes, it is possible and here it goes.
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
Hi Jamil,
Thanks for all your good help here. I copied your COUNT VBA but there is problem hope you can help me with:
if the conditional formatting is done by simple formula like >50 e.g. it works.
But my conditional formatting is done with this formula: =AND(T3<=$K3,T3>=($K3-$L3+1)).
Now it doesn't work anymore.
Can you help out?
Thanks a million!
- JamilApr 12, 2019Bronze Contributorhiepkt12
this is not the sub routine i wrote. and it will not work for this purpose. you may need to use the function which I wrote to make it work. so, i cannot change that sub to function to make it work. - JamilApr 12, 2019Bronze Contributoryi513
your formula to evaluate a condition is not correct. you cannot have multiple evaluation in the single condition. for example =IF(N$5<$L6, IF(N$5>=$K6, TRUE, FALSE), FALSE) is not correct. try to see how you can first put correct condition into the workbook. Also, please read my earlier posts on the limitation of UDF and also the troubleshootings of other users with similar problems. - hiepkt12Apr 09, 2019Copper Contributor
I found this code to be used in my file but I want to switch from "Sub" to "Function". That is, I want the result of the yellow box displayed in the column count color yellow.
Help me!!
Thank you very much!!
Sub DisplayFormatCount()'Updateby20150305Dim Rng As RangeDim CountRange As RangeDim ColorRange As RangeDim xBackColor As LongDim xFontColor As LongOn Error Resume NextxTitleId = "KutoolsforExcel"Set CountRange = Application.SelectionSet CountRange = Application.InputBox("Count Range :", xTitleId, CountRange.Address, Type: = 8)Set ColorRange = Application.InputBox("Color Range(single cell):", xTitleId, Type: = 8)Set ColorRange = ColorRange.Range("A1")xReturn = 0For Each Rng In CountRangeqqq = Rng.Valuexxx = Rng.DisplayFormat.Interior.ColorIf Rng.DisplayFormat.Interior.Color = ColorRange.DisplayFormat.Interior.Color ThenxBackColor = xBackColor + 1End IfIf Rng.DisplayFormat.Font.Color = ColorRange.DisplayFormat.Font.Color ThenxFontColor = xFontColor + 1End IfNextMsgBox "BackColor is " & xBackColor & Chr(10) & "FontColor is " & xFontColorEnd Sub - JamilMar 29, 2019Bronze ContributorHi yi513
It is because the range you used in the UDF and the range set in CF are not matching.
example Range used for column N is N$6:N$205 while CF set is =$N$6:$BJ$205
plus the the condition of UDF color is in C3 where none of the colors in the range $N$6:$BJ$205 matches that color.
please read my earlier post where I have troubleshooted other users similar problems and that will give you guide on how to solve the issue on your workbook. - yi513Mar 26, 2019Copper Contributor
Hi Jamil,
I tried your code but it returns "0"s. Could you be so kind and take a look at my sample file?
Thank you!