Jump to content

Recommended Posts

Posted

Hi,

Thank you for taking the time to read and help !

I've found how to do what I need to do on this webpage here. The only thing is That I don't figure out how to make it work.

Here's the code:

Function CountByColor(InRange As Range, _

WhatColorIndex As Integer, _

Optional OfText As Boolean = False) As Long

'

' This function return the number of cells in InRange with

' a background color, or if OfText is True a font color,

' equal to WhatColorIndex.

'

Dim Rng As Range

Application.Volatile True

For Each Rng In InRange.Cells

If OfText = True Then

CountByColor = CountByColor - _

(Rng.Font.ColorIndex = WhatColorIndex)

Else

CountByColor = CountByColor - _

(Rng.Interior.ColorIndex = WhatColorIndex)

End If

Next Rng

End Function

They says on the website that I can call the fonction using a formula like this one:
=COUNTBYCOLOR(A1:A10,3,FALSE)

I do not know if the code is at the right place (see print screen):

post-29668-1141534871_thumb.jpg

I call the formula this way:

post-29668-1141534861_thumb.jpg

And I get this error:

post-29668-1141534851_thumb.jpg

Can someone help me plz


Posted (edited)

It looks right.

I don't have Excel installed, so I cannot try it directly, but first thing (as you need - from the other thread to just count cell interiors, NOT text) would be to simplify the function:

Function CountByColor(InRange As Range, WhatColorIndex As Integer) As Long

'

' This function return the number of cells in InRange with

' a background color equal to WhatColorIndex.

'

Dim Rng As Range

Application.Volatile True

For Each Rng In InRange.Cells

CountByColor = CountByColor - (Rng.Interior.ColorIndex = WhatColorIndex)

Next Rng

End Function

But I am failing to see how the above works (CountByColor is not initialised)....

You can try the following (untested as said above):

Function CountByColor(InRange As Range, WhatColorIndex As Integer) As Long

'

' This function return the number of cells in InRange with

' a background color equal to WhatColorIndex.

'

Dim Rng As Range

Application.Volatile True

CountByColor = 0

For Each Rng In InRange.Cells

IF Rng.Interior.ColorIndex = WhatColorIndex Then

CountByColour = CountByColour + 1

End If

Next Rng

End Function

jaclaz

Edited by jaclaz

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...