+ Reply to Thread
Results 1 to 7 of 7

UDF to count colored cells always gives zero

  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    UDF to count colored cells always gives zero

    I want to count the number of colored cells in a column but I keep getting zero even when there are colored cell present

    Thanks

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: UDF to count colored cells always gives zero

    I suspect it will be very difficult for us to debug this for you without a sample spreadsheet to test the code on. Since debugging is a very important skill (certainly important enough to develop some debugging skills of your own), I would suggest the following:

    1) If you are unfamiliar with the debugging tools available in VBA and how to use them, I would suggest reviewing a page like this: http://www.cpearson.com/excel/DebuggingVBA.aspx

    2) I would probably start debugging this by adding a break point at the end with statement in sub icountcolors() (or a stop statement after the end with). Run the procedure up to this point. When VBA enters debug mode, I will be looking for:
    a) what value did it return for LR?
    b) perhaps more interestingly, what range is stored in Rng? This might be easiest by setting a watch for Rng.address. By whatever means you devise, the idea is to make sure that Rng refers to the range that you expect it to. If it does, then move on. If not, then you need to figure out what went wrong during the block with.

    3) Set a stop of breakpoint in countcolors before it does anything.
    4) check to make sure that RangeToCount contains the range you expect it to contain.
    5) Step through the function. (or stop after the If...Then statment inside of the loop).
    a) What cell is "colorcell" referring to on this pass through the loop?
    b) Check the value returned by colorcell.interior.colorindex. Is it an RGB value that you would expect, knowing the color that is in each cell?
    6) Go back to 5 and continue stepping through the loop (you will probably want to perform this on a small test range), checking the values for each colorcell.

    For countcolors to return 0, it seems that it must never actually return True for the If test inside of this loop. The overall idea for the second part of this is to see if you can see why it is never seeing this test as true.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: UDF to count colored cells always gives zero

    Please Login or Register  to view this content.
    ColorIndex is represented by a single numeric value. RGB() should use .Color instead of .ColorIndex

    In addition, if you are using conditional formatting it will not detect the color.
    If you are happy with my response please click the * in the lower left of my post.

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: UDF to count colored cells always gives zero

    Please Login or Register  to view this content.
    As far as I can see you are only checking for interior color pink all other colors are ignored.

    Perhaps this link could be of interest

    http://www.excelforum.com/excel-prog...lor-cells.html

    Alf

  5. #5
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: UDF to count colored cells always gives zero

    Thank you all for your help, I thought I did upload a file I did not notice it did not take.

    Turns out that this
    If ColorCell.Interior.ColorIndex = RGB(255, 0, 255)
    needed to be this
    If ColorCell.Interior.Color = RGB(255, 0, 255)
    as stynknts said

    Also
    if you are using conditional formatting it will not detect the color
    which I am

    Is there a way to count the cell colors when "conditional formatting" ?

    I have uploaded a file with what I am doing
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: UDF to count colored cells always gives zero

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: UDF to count colored cells always gives zero

    Thank you stynknts that sorted it

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to count colored cells?
    By cat3appr in forum Excel General
    Replies: 7
    Last Post: 10-07-2015, 06:17 PM
  2. Replies: 1
    Last Post: 11-09-2013, 02:43 AM
  3. Don't Count Cells Colored Red
    By scotfitz in forum Excel General
    Replies: 1
    Last Post: 04-20-2008, 04:19 PM
  4. Count colored cells?
    By mlk in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-30-2007, 06:30 PM
  5. count colored cells
    By apx2001 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-09-2006, 01:00 PM
  6. count colored cells?
    By DKY in forum Excel Formulas & Functions
    Replies: 21
    Last Post: 01-19-2006, 05:50 PM
  7. How to Count only Colored cells
    By WM_1956 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2005, 09:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1