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.
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.
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.
Originally Posted by shg
ColorIndex is represented by a single numeric value. RGB() should use .Color instead of .ColorIndexPlease Login or Register to view this content.
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.
As far as I can see you are only checking for interior color pink all other colors are ignored.Please Login or Register to view this content.
Perhaps this link could be of interest
http://www.excelforum.com/excel-prog...lor-cells.html
Alf
Thank you all for your help, I thought I did upload a file I did not notice it did not take.
Turns out that thisneeded to be thisIf ColorCell.Interior.ColorIndex = RGB(255, 0, 255)as stynknts saidIf ColorCell.Interior.Color = RGB(255, 0, 255)
Alsowhich I amif you are using conditional formatting it will not detect the color
Is there a way to count the cell colors when "conditional formatting" ?
I have uploaded a file with what I am doing
Please Login or Register to view this content.
Thank you stynknts that sorted it
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks