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
 
I have both:

CountByColor:

Option Explicit

Public Function CountByColor(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
CountByColor = Count
End Function
Function CFColour(Cl As Range) As Double
CFColour = Cl.DisplayFormat.Interior.Color
End Function


CountByColor2:

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


Which one do you want me to try in the cell?
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I am referring to the code I posted, not your code. Is it in a standard module?
 
Upvote 0
The code that you send I first changed it from what it was to your code in CountByColor
 
Upvote 0
Will you please answer my question.
 
Upvote 0
I think so, it is under Modules versus Class Modules

1651079218492.png


I think that is what you mean?
 
Upvote 0
That's fine. Is the other code I supplied in the same module?
 
Upvote 0
In that case I have no idea why it's not working.
Can you post some sample data including the formula your using.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
I think the problem is that you cannot use DisplayFormat directly in a UDF. There is a workaround though. Remove whatever color counting functions you now have and put the following two functions in their place...
VBA Code:
Function CountByColor(CellRange As Range, TargetCell As Range) As Long
  CountByColor = Evaluate("CountColorHelper(" & CellRange.Address & "," & TargetCell.Interior.Color & ")")
End Function

Function CountColorHelper(CellRangeToCount As Range, ColorToCount As Long) As Long
  Dim TargetColor As Long, Count As Long, C As Range
  For Each C In CellRangeToCount
    If C.DisplayFormat.Interior.Color = ColorToCount Then Count = Count + 1
  Next C
  CountColorHelper = Count
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,904
Members
449,194
Latest member
JayEggleton

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