udf macro which can count Conditional formatting?

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
510
Office Version
  1. 365
Platform
  1. Windows
hi,
is there's udf macro that can count cells in conditional formatting, no matter the color?
i tried google for it, even find some in here, but none with what i need
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Not sure if this is what you meant
VBA Code:
Sub CountCF()

Dim cell As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
 
Set cell = ws.Cells.SpecialCells(xlCellTypeAllFormatConditions)
Debug.Print cell.Count

End Sub
 
Upvote 0
thanks zot,
i meant this

test.xlsx
ABCDEFGHIJK
12345678how many colored cells?
25
3a1:e1 bigger then 2equal to 8
4
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G1Cell Value=8textNO
F1Cell Value=8textNO
A1:E2,A4:E25,A3:B3,E3Cell Value>2textNO
 
Upvote 0
Oh.. it is not Conditional Formatting but count colored cell

Book1
ABCDEFGHI
12345678how many colored cells?
25
3a1:e1 bigger then 2equal to 8
Sheet1
Cell Formulas
RangeFormula
I2I2=nColored(A1:G1)


The UDF
VBA Code:
Function nColored(rng As Range)
Dim cell As Range
For Each cell In rng
    If Not cell.Interior.ColorIndex = -4142 Then nColored = nColored + 1
Next
End Function
 
Upvote 0
Hi,
Range.DisplayFormat property may do what you want - however, it does not work inside a UDF
There are are couple of workarounds that have been published


or


you may find helpful

Dave
 
Upvote 0
Thanks @dmt32. I misunderstood him.

My 1st solution wasn't thinking much. I was actually counting all Conditional Formatting on a sheet.

So, from the references you provided, my UDF is
Book1
ABCDEFGHI
12345678how many colored cells?
25
3a1:e1 bigger then 2equal to 8
Sheet1
Cell Formulas
RangeFormula
I2I2=nCFColor(A1:G1)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G1Expression=$G$1=8textNO
A1:E1Expression=A$1>2textNO

VBA Code:
Function nCFColor(rng As Range) As Long
Dim x As Double
Dim cell As Range
Application.Volatile
For Each cell In rng
    x = Evaluate("CFColor(" & cell.Address() & ")")
    If x <> 16777215 Then nCFColor = nCFColor + 1
Next cell
End Function

Function CFColor(c As Range) As Double
CFColor = c.DisplayFormat.Interior.Color
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,215,004
Messages
6,122,656
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