army20 Posted March 5, 2006 Posted March 5, 2006 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 RangeApplication.Volatile TrueFor Each Rng In InRange.CellsIf OfText = True Then CountByColor = CountByColor - _ (Rng.Font.ColorIndex = WhatColorIndex)Else CountByColor = CountByColor - _ (Rng.Interior.ColorIndex = WhatColorIndex)End IfNext RngEnd FunctionThey 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):I call the formula this way:And I get this error:Can someone help me plz
jaclaz Posted March 5, 2006 Posted March 5, 2006 (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 RangeApplication.Volatile TrueFor Each Rng In InRange.CellsCountByColor = CountByColor - (Rng.Interior.ColorIndex = WhatColorIndex)Next RngEnd FunctionBut 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 RangeApplication.Volatile TrueCountByColor = 0For Each Rng In InRange.CellsIF Rng.Interior.ColorIndex = WhatColorIndex ThenCountByColour = CountByColour + 1End IfNext RngEnd Functionjaclaz Edited March 5, 2006 by jaclaz
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now