counting by conditional formatting color

tbilderbac

New Member
Joined
Apr 11, 2022
Messages
28
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I got this code:

-----------
Public Function CountByColor2(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = TargetCell.Interior.ColorIndex
For Each C In CellRange
If C.Interior.ColorIndex = TargetColor Then Count = Count + 1
Next C
CountByColor2 = Count


End Function
------------

I created the formula:
=CountByColor2(RC[-23]:RC[-6],R[-16]C[-25])

and wanting it to count up the RED blocks: [should be 5]

1651059535408.png
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You'll need to add "DisplayFormat" to account for the fact that the formatting is surface level through conditional formatting. soooo...

VBA Code:
Public Function CountByColor2(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = TargetCell.DisplayFormat.Interior.ColorIndex
For Each C In CellRange
         If C.DisplayFormat.Interior.ColorIndex = TargetColor Then Count = Count + 1
Next C
CountByColor2 = Count

End Function
 
Upvote 0
Is the target cell manually coloured or is that also done with conditional formatting?

@Craggs82 you cannot use DisplayFormat in a UDF. ;)
 
Upvote 0
every day is an annoying Microsoft school day :biggrin:

best option would be re-write the formatting rules in the udf? oof.

edit. typo.
 
Upvote 0
Thank you, the TargetCell is done with Conditional formatting, thought I had to do that to make the code to work? So I just did a quick rule to get the same color for the TargetCell as the row cells. I will add in the DisplayFormat to the string. and try
 
Upvote 0
The DisplayFormat will not work, as you cannot use it when calling the function from the sheet.
 
Upvote 0
So I copied the code:

------
Option Explicit

Public Function CountByColor2(CellRange As Range, TargetCell As Range)

Dim TargetColor As Long, Count As Long, C As Range

TargetColor = TargetCell.DisplayFormat.Interior.ColorIndex
For Each C In CellRange
If C.DisplayFormat.Interior.ColorIndex = TargetColor Then Count = Count + 1
Next C
CountByColor2 = Count


End Function
------
and still not working?

=CountByColor(RC[-23]:RC[-6],R2C5)

1651065380631.png
 
Upvote 0
You can use this workround, although you would probably be better of with a normal formula that uses the same rules as your CF
VBA Code:
Public Function CountByColor2(CellRange As Range, TargetCell As Range)

   Dim TargetColor As Long, Count As Long, C As Range
   
   TargetColor = Evaluate("cfcolour(" & TargetCell.Address & ")")
   For Each C In CellRange
      If Evaluate("cfcolour(" & C.Address & ")") = TargetColor Then Count = Count + 1
   Next C
   CountByColor2 = Count


End Function
Function CFColour(Cl As Range) As Double
   CFColour = Cl.DisplayFormat.Interior.Color
End Function
 
Upvote 1
Not working, could it be it is not working because I have excel 365?
 
Upvote 0
In what way isn't it working?
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,721
Members
449,465
Latest member
TAKLAM

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