+ Reply to Thread
Results 1 to 11 of 11

Thread: Counting colors using VBA

  1. #1
    Registered User
    Join Date
    11-04-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Counting colors using VBA

    So, after doing much reasearch, I got my sheet to work. I am using VBA to count colors. I am using this:

    =ColorFunction(B2,STAFF!D2:STAFF!D61)

    This counts all the red cells. B2 is manually filled red (a controll cell). I have dates is colum D2:D61. I am using conditional formating to color them. Such as:

    =(DATEDIF(D2,$C$77,"m")/12)>0.95

    I have it set to fill the cell red. It works well. If I hit CRL + ALT + F9 it updates the macro if I manually change a cell's fill color. All is well until I try to update the sheet. I am exporting my raw data from MS Access to Excell. The copy and paste into my template with the above formulas. Once I do that the color if fine but the macro no longer works.

    What gives?
    Last edited by Wilburt; 11-05-2010 at 10:01 AM.

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,225

    Re: Counting colors using VBA

    You don't say where you got the ColorFunction from or what, exactly it does.

    In my experience, these functions count true formatted colours, not colours induced by Conditional Formatting.

    What are you counting?

    Regards

  3. #3
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,225

    Re: Counting colors using VBA

    Oh, and you'd need to make the function volatile so that it responds to changes in the sheet and recalculates.

    Regards

  4. #4
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Counting colors using VBA

    Do you mean this?
    =DATEDIF($D2,$C$77,"y")>=1

    If so you could use a helper column, say G, which can be hidden if you want.
    In G2
    =DATEDIF($D2,$C$77,"y")
    Drag/Fill down to G62

    Then
    =COUNTIF($G$2:$G62,">=1")

    This would be far simpler than trying to count cell colours that are applied with conditional formatting, that can become quite complicated.

    Hope this helps
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  5. #5
    Valued Forum Contributor alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Lake County, Illinois
    MS-Off Ver
    MS Office 2010, 2007 and 2002
    Posts
    1,165

    Re: Counting colors using VBA

    An alternative to your function is this one which you may be able to use and then do a countif function.

    http://www.datapigtechnologies.com/f...rtonColor.html

    Alan

  6. #6
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Counting colors using VBA

    Quote Originally Posted by Wilburt View Post
    I have dates is colum D2:D61. I am using conditional formating to color them. Such as:

    =(DATEDIF(D2,$C$77,"m")/12)>0.95

    I have it set to fill the cell red.
    As inferred by others - if you have logic determining the colour then ape the logic in your count formula - no need for VBA at all.

    =SUMPRODUCT(ISNUMBER(D2:D61)*(DATEDIF(STAFF!D2:D61,$C$77,"m")/12>0.95))
    we're assuming no text values can be added to the range - if they can then an Array would be preferable

    Quote Originally Posted by TMS
    ...you'd need to make the function volatile so that it responds to changes in the sheet and recalculates.
    The precedent ranges are explicit so as they recalculate so too the UDF.

    Altering the format of a cell manually (B2) is not a Volatile action so making the UDF volatile would not resolve that issue .. though it would reduce the exposure to error given the UDF would calculate upon the next volatile action irrespective of precedent ranges.

  7. #7
    Registered User
    Join Date
    11-04-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Counting colors using VBA

    As inferred by others - if you have logic determining the colour then ape the logic in your count formula - no need for VBA at all.
    I just figured that out. I am now using:

    =COUNT(STAFF!D3:D300)-COUNTIF(STAFF!D3:D300,"<"&STAFF!C1-365)

    C1=my current date. This counts dates that are less than a year old. I do this instead of VBA. This fixed my problem. I do have another question. Which would be more acurate?

    =(DATEDIF(D2,$C$77,"m")/12)>0.95

    or

    =DATEDIF($D2,$C$77,"y")>1

    I was using .95 because, if I used 1, some of the dates that were a month out of tollerance would not color red with conditional formating.

  8. #8
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Counting colors using VBA

    Do you mean > 1 or >= 1 ?

    It might be an idea to provide some examples of the dates you felt were not generating the appropriate formatting.

    On an aside if you're using XL2007 or above you might also consider use of EDATE to work dates backwards
    (pre XL2007 EDATE requires activation of Analysis ToolPak Add-In on each client PC that will use the file)

  9. #9
    Registered User
    Join Date
    11-04-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Counting colors using VBA

    I attached an example. Not sure of the difference in the = sign. As you can see the first table fails to shade 4 dates that are in the month past a year old. Also, I am using 2007, but some users that will access it might be using the compatibility one (2003-2007)?

    Edit to add: The formula in the example above the table should read C1 and D4 not C77 and D2.
    Attached Files Attached Files
    Last edited by Wilburt; 11-05-2010 at 09:58 AM. Reason: Add

  10. #10
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Counting colors using VBA

    Quote Originally Posted by Wilburt
    Not sure of the difference in the = sign
    >1 means greater than one - in excess of one year

    >=1 means greater than or equal to one - one year or more

    If you modify the first table to use >= rather than > you will get the same results as the second table.

  11. #11
    Registered User
    Join Date
    11-04-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Counting colors using VBA

    Thanks! Will mark the thread solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0