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
Sonia1640
Apr 15, 2019Copper Contributor
Jamil
Hi Jamil,
Thank you. Your replies to this post have helped me so much!
I used your code and included Application.volatile. But, I can't get my spreadsheet to automatically calculate the number of coloured cells. It will only update the number of cells if I edit the cell in the formula bar. Is there a way I can set up a button on my spreadsheet that will force excel to calculate how many cells have been coloured?
Jamil
Apr 15, 2019Bronze Contributor
Hi Sonia1640
Please check that your workbook setting calculation mode is "Automatic"
on the Formula Tab click on "Calculation Option" and from the dropdown plz make sure "Automatic" is selected. If it is Manual then change it to Automatic.
Please check that your workbook setting calculation mode is "Automatic"
on the Formula Tab click on "Calculation Option" and from the dropdown plz make sure "Automatic" is selected. If it is Manual then change it to Automatic.
- JamilApr 15, 2019Bronze ContributorHi Sonia,
Plz check if you have a personal excel file in the Excel start up folder. Also, check if you have any Excel Add-ins enabled.
If the above-mentioned did not work. then place this code below into sheet module not in a regular module.
You can change the range A2:Z100 to the actual range where your formulas are placed.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Range("A2:Z100").Calculate
End Sub - Sonia1640Apr 15, 2019Copper ContributorHi Jamil,
The workbook is set to automatic. Do you have any other ideas?