UDF Count the number of cells coloured by conditional formatting

Claire Jackson

Board Regular
Joined
Jun 30, 2020
Messages
79
Office Version
  1. 2016
Platform
  1. Windows
I've been searching for days on this and finally have had to ask for help. I can see its been asked many times before but each example I find doesn't work.

I have a spreadsheet with part of it conditionally formatted with orange based on certain criteria. All I want to do is count the amount of coloured cells and I just can't get it to work.

The cell colour I am looking for is in cell BP1 and the formula in BP2 is this: =@CountConditionColorCells($K$13:$BN$22,BP1)

the code I have is this:

VBA Code:
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
 

Attachments

  • Resource.jpg
    Resource.jpg
    161.4 KB · Views: 61
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
The first two are. The range used to split into two colours based on a percentage but we don't need that now so rather than just messing with formulas we just turned them both to orange.
 
Upvote 0
What are task_start, task_end & task_progress?
 
Upvote 0
Hi,

I've removed the first conditional format and the only that turns orange now is this one:
=AND(task_end>=K$10,task_start<L$10)

Task start is just a start date of a task and Task end is when the task is due to finish
 
Upvote 0
In that case you can just use
Excel Formula:
=COUNTIFS($K$10:$BN$10,">="&task_start,$K$10:$BN$10,"<="&task_end)
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0
Try
Excel Formula:
=COUNTIFS(task_start,"<="&K10,task_end,">="&K10)
 
Upvote 0
Hi sorry that doesn't work. I'll explain what I want to do.

Please see screenshot and what I need to do is total the amount of conditional formatted "orange" cells above the data. e.g. count all the bricklayer oranges and pop above for that particular day and same for joiners, plasterers etc.
 

Attachments

  • SS.jpg
    SS.jpg
    145 KB · Views: 14
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top