Counting cell colours that are coloured via CF

tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
781
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a range of values (B2:B11) that have multiple Conditional Formatting (CF) rules, I am now trying to make a UDF that will count how many cells are orange for example.

I have used the following procedure to decipher what the colour index is of the orange cell:

Code:
Sub ColourMessage()
    MsgBox ActiveCell.DisplayFormat.Interior.ColorIndex
End Sub

Which returns 44 when I test, this is fine, so I have then made the following but it doesn't quite work:

Code:
Function COUNTORANGE(rng As Range)
    Dim cell As Range
        COUNTORANGE = 0
        For Each cell In rng
            If cell.Interior.ColorIndex = 44 Then
                COUNTORANGE = COUNTORANGE + 1
            End If
        Next cell
End Function

This returns 0 even though all of the cells are orange for testing purposes.

If I change
Code:
If cell.Interior.ColorIndex = 44 Then
to:
Code:
If cell.DisplayFormat.Interior.ColorIndex = 44 Then

I get #VALUE ! when I use the function.

With B2:B11 CF'd to orange, I am expecting to see =COUNTORANGE(B2:B11) return 10. Any ideas?

Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You forgot "DisplayFormat" in your UDF, i.e.
Code:
Function COUNTORANGE(rng As Range)
    Dim cell As Range
        COUNTORANGE = 0
        For Each cell In rng
            If cell.[COLOR=#ff0000]DisplayFormat.[/COLOR]Interior.ColorIndex = 44 Then
                COUNTORANGE = COUNTORANGE + 1
            End If
        Next cell
End Function
you had it in your test Procedure, but left it out in the UDF.
 
Last edited:
Upvote 0
You forgot "DisplayFormat" in your UDF, i.e.
Code:
Function COUNTORANGE(rng As Range)
    Dim cell As Range
        COUNTORANGE = 0
        For Each cell In rng
            If cell.[COLOR=#ff0000]DisplayFormat.[/COLOR]Interior.ColorIndex = 44 Then
                COUNTORANGE = COUNTORANGE + 1
            End If
        Next cell
End Function
you had it in your test Procedure, but left it out in the UDF.

Hi Joe,

Thanks for the response, but in my original post I stated that I had also tried with that line in the UDF, unfortunately it only returns an error. (#VALUE!)
 
Upvote 0
It seems to work for me when used within VBA, but not directly on the worksheet (not sure why).

So this worked for me:
Code:
Sub tester()
    Dim rng As Range
    Set rng = Range("B2:B11")
    MsgBox COUNTORANGE(rng)
End Sub
Not sure why it these UDF falls over itself when called directly on the worksheet...

EDIT: Looks like Fluff knows the answer!
 
Last edited:
Upvote 0
BTW, sorry about overlooking the end of your orignal. I skimmed your original question, and copied your code, and was messing around with it and testing it all in VBA.
 
Upvote 0
BTW, sorry about overlooking the end of your orignal. I skimmed your original question, and copied your code, and was messing around with it and testing it all in VBA.

Not to worry Joe, I skim read and miss things all the time too :laugh:! The procedure you posted does indeed work but for a function, sadly it doesn't, as Fluff has mentioned also.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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