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.
Hi Fenfen,
you have given the range of cell into the UDF range arguments cells that do not have conditional formatting applied to them.
UDF will throw error if range of cell given as input that do not have conditional formatting rule applied to it.
so to make your workbook work. you could achieve the result by using a combined addition
=COUNTConditionColorCells(B3:I3,J2)+COUNTConditionColorCells(B8:I8,J2)+COUNTConditionColorCells(B13:I13,J2)++COUNTConditionColorCells(B18:I18,J2)
see attached file.
H
Jamil wrote:Hi Fenfen,
you have given the range of cell into the UDF range arguments cells that do not have conditional formatting applied to them.
UDF will throw error if range of cell given as input that do not have conditional formatting rule applied to it.
so to make your workbook work. you could achieve the result by using a combined addition
=COUNTConditionColorCells(B3:I3,J2)+COUNTConditionColorCells(B8:I8,J2)+COUNTConditionColorCells(B13:I13,J2)++COUNTConditionColorCells(B18:I18,J2)see attached file.
Hi Jamil,
I have tried, but its not working. cause my the color will be change anytime. please see sample file.
- JamilApr 25, 2018Bronze ContributorHi Fenfen,
It is because the conditional formatting formula of Vlookup uses the same range that is used in the formula on the cells that produce the numeric results, so it is kinda create circular reference in a nontraditional way. The UDF is not designed to handle this sort of complex scenario. the UDF works on the simple conditional formatting that would use a formula that evaluate cells that has either simple values or if they have formulas, they are not using the same range as the formula used in the conditional formatting.
I suggest you use the other methods described in the earlier posts, like combination of SUMPRODCUCT with COUNTIFS- Prasanth KunasingamMay 22, 2018Copper Contributor
Hi Jamil,
How do I use the UDF to work on all the excel files by default, not just a specific one? Is there a way to set it up under personal macro file?
- EquiliMario YoutubeJun 23, 2018Copper Contributor
Hi Jamil,
I created a test worksheet (highlighted yellow) in my workbook to test the UDF and it works, however it counts one too high for one condition. Could you look at this?
Also it appears that the UDF cannot handle my Conditional Formatting formula that I want to check. It uses the INDIRECT function to determine the cell that needs to be checked instead of a direct reference. I highlighted this worksheet green and put the color codes in the reference formatted cells. I don't think I can find a way to create a less complicated Conditional Formatting formula. If I want to rework the formula I need to move a lot of data and clutter the worksheets which I would like to avoid.
Could the UDF be modified to recognize INDIRECT functions?
Your dedication to this subject is outstanding btw :)
Mario
- JamilJun 25, 2018Bronze Contributor
Hi Mario,
the colors you selected for background are not the standard colors, so if you select from one of the standard colors, for example. change the background color of Past due to red. then the UDF will result correct output.
plz see attached.