+ Reply to Thread
Results 1 to 11 of 11

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 TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    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
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    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 (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Counting colors using VBA

    Do you mean this?
    Please Login or Register  to view this content.

    If so you could use a helper column, say G, which can be hidden if you want.
    In G2
    Please Login or Register  to view this content.
    Drag/Fill down to G62

    Then
    Please Login or Register  to view this content.

    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 select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,859

    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
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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.

    Please Login or Register  to view this content.
    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 Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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 Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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.6.0 RC 1