COUNTING CONDITIONALLY FORMATTED COLOURED CELLS USING VBA

J_MAW

New Member
Joined
Sep 9, 2015
Messages
37
Hi

Hoping that you can help!

I've applied a VBA code that I got off the internet to help me count conditionally formatted coloured cells.

I only want to count "red" cells by individual row.

The VBA / Formula I am using is returning "NOCOLOR".

Could someone possibly explain where I am going wrong. Sample file can be accessed here Final Reference Tracker sample.xlsm

Thank you in advance :)

JMAW
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
It might be better off if you post the relevant code here if you can - quite a few people might be a little reluctant to download a workbook with VBA in it when they don't know the author, etc. :)
 
Upvote 0
Thanks Dan

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

I have a reference tracker and if a date "expires" the cell is conditionally formatted to red. Each colleague has a row in the sheet, and I only want to count the red cells for each individual colleague by row not the whole sheet.

I have set the VBA up then used the formula =@COUNTConditionColorCells(H2:BN2,B2) (B2 refers to a cell that has the same colour red in as the conditionally formatted cells).

I'm not experienced in VBA at all so haven't set up a macro (not experienced in these either). Is this why it's not working?

Many thanks,

JW
 
Last edited by a moderator:
Upvote 0
Is the B2 cell manually filled or by conditional formatting?
 
Upvote 0
Hi @Fluff - Column B is currently filled with 0, which triggers a conditional formatting rule to turn the cell red. I suspect it's just being used to try and debug the VBA code, but I might be wrong on that.

Final Reference Tracker sample.xlsm
ABCDEFGHIJKLMNOPQRST
1Full NameColorChasers DueType of ReferenceStart DateEnd DateREF 01. StatusDateType of ReferenceContact Tel: NoStart DateEnd DateREF 02. StatusDateType of ReferenceContact Tel: NoStartDateEnd DateREF 03. StatusDate
2R J#VALUE!British Airways07/07/200729/08/2009Requested13-Jul-22Cgroup30/08/200901/04/2010Requested13-Jul-22Airport hotel02/04/201001/05/2010Requested13-Jul-22
3A L#VALUE!British Airways13/02/199731/10/2012Received13-Jul-22Cgroup22/11/201215/01/2014Received13-Jul-22Airport hotel02/04/201001/05/2010Received13-Jul-22
4L T#VALUE!Class 1 Personnel12/11/200709/05/2010Chaser12-Jul-22Cgroup10/05/201017/11/2011Chaser12-Jul-22Airport hotel09/01/201230/03/2012Chaser12-Jul-22
5A W#VALUE!Class 1 Personnel15/09/200923/06/2009Requested13-Jul-22Cgroup06/07/201003/02/2011Requested13-Jul-22Airport hotel10/06/201006/07/2010Requested13-Jul-22
6C C#VALUE!Class 1 Personnel06/06/200901/12/2009Received13-Jul-22Cgroup14/12/200905/04/2010Received13-Jul-22Airport hotel06/04/201022/08/2010Received13-Jul-22
7G B#VALUE!Class 1 Personnel19/11/200914/06/2011Chaser12-Jul-22Cgroup14/06/201110/11/2011Chaser12-Jul-22Airport hotel10/11/201115/04/2013Chaser12-Jul-22
8A W#VALUE!Class 1 Personnel01/01/200918/02/2009Requested13-Jul-22Cgroup19/02/200919/06/2011Requested13-Jul-22Airport hotel01/07/201104/12/2013Requested13-Jul-22
9
Reference
Cell Formulas
RangeFormula
C2:C8C2=COUNTConditionColorCells(H2:BN2,B2)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
N2:N8Expression=IF(OR(M2="Requested",M2="Chaser"),AND(N2<TODAY()-7))textNO
T2:T8Expression=IF(OR(S2="Requested",S2="Chaser"),AND(T2<TODAY()-7))textNO
B2:B8Cell Value=0textNO
H2:H14Expression=IF(OR(G2="Requested",G2="Chaser"),AND(H2<TODAY()-7))textNO
Cells with Data Validation
CellAllowCriteria
S2:S9ListSelect, Requested, Received, Chaser
G2:G9ListSelect, Requested, Received, Chaser
M2:M9ListSelect, Requested, Received, Chaser


My gut instinct is that applying conditional formatting (and then counting the number of conditionally formatted cells) is not the best/most efficient method to accomplish what J MAW is trying to do, but please do let me know what you think. :)
 
Upvote 0
Totally agree, colour is not data & should not be used as such.
 
Upvote 0
@J_MAW rather than using a function, you can use a simple formula
Excel Formula:
=SUM(COUNTIFS(G2:BM2,{"Requested","Chaser"},H2:BN2,"<"&TODAY()-7))
 
Upvote 0
Solution
Conditional formatting - please see below. Thank you
Sample 3.png
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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