+ Reply to Thread
Results 1 to 8 of 8

Conditional Formatting by VBA

  1. #1
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Conditional Formatting by VBA

    Hi All,
    I asked, on the Forum, for help regarding colouring 'birthday' cells based on the Month of the year.
    The answer, and solution, was excellent (even though I'm not to sure how it works).
    Now, I've been asked to sort the same birthday list by the Age of the person.
    Unfortunately, the current Conditional Formatting by Months does not work and colours the cells randomly.

    My thoughts are to change the colours of the cells using VBA.
    Colour the cells when sorted by Months, when sorted by Age, just remove the formatting and colour the cells light green (for example).

    I've attached a sample couple of Worksheets to show what happens - (TrialSheet) and what I've been trying to do with VBA (DevSheet).

    Any advice would be appreciated.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,718

    Re: Conditional Formatting by VBA

    What do you want it to look like when sorted by age??? It probably doesn't need VBA.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Contributor frabulator's Avatar
    Join Date
    09-16-2015
    Location
    United States
    MS-Off Ver
    2019
    Posts
    101

    Re: Conditional Formatting by VBA

    I agree with AliGW. It sounds like you would not really need VBA for this.

    It sounds like your issue is arising from not selecting all the data before you sort.

    Please see this image for reference.

    Select ALL of your data (including the hidden cells), then go to Data> Sort> Sort By (Column L) - Sort On (Cell Values) - Order (Smallest to Largest)> OK

    Capture02.JPG

    ---

    You also have the option to record your own macros so you can see how they work, just in case you did not know about this. This tool is under Developer> Record.

    Recording the example above yielded this result.

    Please Login or Register  to view this content.
    Last edited by frabulator; 04-11-2021 at 07:34 AM.

  4. #4
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Conditional Formatting by VBA

    Thanks AliGW, for looking at my problem.

    When sorted by Age, I just want all the cells containing Dates and Names to be one colour, or even no colour come to that.
    I can clear the Dates and Names using VBA when sorting by Age by using something like - "(Range("G3:H35").Interior.ColorIndex = 0"
    Getting the formatting back again when sorting by Month using "=ISODD(SUMPRODUCT(1/COUNTIF($E$3:$E3,$E$3:$E3)))" and "=ISEVEN(SUMPRODUCT(1/COUNTIF($E$3:$E3,$E$3:$E3)))" in conditional formatting, is my issue.

    Hope i'm making sense?

  5. #5
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Conditional Formatting by VBA

    Thanks frabulator,

    I know about the recording of macros but using that method puts the blank rows, from 27 to 35, at the top of the sorted list.
    How can I convert the line - .SetRange Range("E3:J35") to only include down to line 26?
    i've used something like - "lRowG = sht.Cells(sht.Rows.Count, 7).End(xlUp).Row" to find the last row with data in col G, but cannot work out how to put the same
    on the ".SetRange Range("E3:J35")" line in the macro.

    Thanks

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,718

    Re: Conditional Formatting by VBA

    How about this?

    =AND($G$1="Sorted by: Month",ISEVEN(SUMPRODUCT(1/COUNTIF($E$3:$E3,$E$3:$E3))))

    =AND($G$1="Sorted by: Month",ISODD(SUMPRODUCT(1/COUNTIF($E$3:$E3,$E$3:$E3))))

    Click your sorting buttons to see the effect.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Conditional Formatting by VBA

    Thanks AliGW,

    Absolutely ideal!

    Many, and I mean many, years ago, I used to use program using Fortran77 at my work.
    Unfortunately I'm physically not able to do much at all now, so I thought, let's try and keep the
    "little grey cells" going and look at Excel (as it came with my comp).
    I realise now, my lack of logic, but thank you, and all like you who help unconditionally.
    It is very much appreciated.

    Thanks once again.

    Andrew

    (p.s. I would never have been able to work out the "=AND($G$1="Sorted by: Month" bit.)

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,718

    Re: Conditional Formatting by VBA

    It's certainly great for the little grey cells here - that's what I come her for, and for the pleasure helping others gives.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who helped.

    PS I come from Sawley.

+ 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. [SOLVED] Conditional Formatting based on another cells Conditional Formatting
    By chriskay in forum Excel General
    Replies: 4
    Last Post: 08-22-2019, 05:33 AM
  2. [SOLVED] Override conditional formatting (in general, without changing the conditional formatting)
    By Stormin' in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-21-2017, 07:15 AM
  3. Conditional Formatting Removing Previous Conditional Formatting?
    By CravingGod in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2016, 01:02 PM
  4. Replies: 6
    Last Post: 01-08-2016, 06:44 PM
  5. Opening xlsm files with conditional formatting opens with removed conditional formatting
    By Martijn.Steenbakker in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-07-2014, 05:38 AM
  6. Replies: 1
    Last Post: 09-20-2013, 06:23 PM
  7. Replies: 3
    Last Post: 05-15-2012, 04:13 PM

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