+ Reply to Thread
Results 1 to 21 of 21

Conditional formatting based on multiple criteria (needs to be in VBA)

  1. #1
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Conditional formatting based on multiple criteria (needs to be in VBA)

    Hi Everyone,

    First off, in case anybody was wondering, I have searched in this forum for anything similar but haven't found anything I can easily modify for my own usage. I'd thought I'd let everyone know just in case someone suggests a search!

    I have a large worksheet which I need conditional formatting on a range of cells based on it's own value and another cell's value on the same row.
    For example, Cell L2 has a date value and cell J2 has a text value.

    I want cell L2 to be highlighted IF the date in this cell is over 15yrs before today's date and cell J2 value is "Conveyancing" OR, if cell J2 is not equal to "Conveyancing" and cell L2 is over 10yrs before today's date to be highlighted also.

    I have achieved this quite well with using the conditional formatting option under Format and it goes like this:

    =IF(AND($J2="CONVEYANCING",$L2<DATE(YEAR(NOW())-15,MONTH(NOW()),DAY(NOW()))),TRUE,IF(AND($J2<>"CONVEYANCING",$2<DATE(YEAR(NOW())-10,MONTH(NOW()),DAY(NOW()))),TRUE,FALSE))

    Now, it isn't that it doesn't work BUT, the workbook doesn't save all the formatting because (after a wee bit of digging around the MSDN forums....) there's a "limitation to how Excel saves information if there are more than 2050 rows with conditional formatting"............hence the need to do this in VBA.

    I am dealing with a BIG sheet of data, around 50,000 rows of information and I need the formatting so I can easily identify rows where it meets the above criteria (and that's just the start!!!!)

    Any help in achieving this in VBA would be mucho appreciated!

    Thanks,
    Tony
    Last edited by Fidd$; 10-12-2009 at 09:12 AM. Reason: added info

  2. #2
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Ok, I've managed to be able to do some tidying up on this and split the data into several sheets. BUT, I will still need to the conditional format in each sheet and as per 1st post, will need VBA as each sheet contains more than 15,000 rows of info.

    Any ideas anyone?

    Thanks!

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Tony, not entirely sure I follow but the below might give you a way forward ?

    Please Login or Register  to view this content.
    To insert the above right click on tab against which the formatting is to be applied select View Code and paste above into resulting window ensuring Macros are enabled thereafter.

    I've obviously not tested the above run against 50000 cells simultaneously and I'm running on a machine with 4GB RAM so performance for me was ok on the limited test I conducted but I can't guarantee the same for you.

  4. #4
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Heyloes, thanks loads for that.
    It'll be a good start and I'll test to see what happens. Running on E8400 Core 2 @ 3Ghz with 2GB ram, bog standard office machines. I'll get back in a bit cos I've a few things to do before I test this, so will give feedback soon as.

    Thanks again,
    Tony

  5. #5
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Hi Donkey,

    It worked a treat! U are a genius tbh

    One thing I forgot to mention: The rows of data are divided up by subtotaling and obviously ranges have totals row under them, is it possible to modify the code so that those rows are not affected by the formatting? i.e. where there is a blank in J2 then nothing to be done to L2.

    I'm going through the code atm trying to figure out what happens and when and am finding it quite difficult to follow through, would you be able to explain to me if possible?

    Thanks loads for this Donkey, you have saved me a lot of time and the file size has reduced considerably as a result of this too! So luvverly jubberly doubly good

    Tony

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Quote Originally Posted by Fidd$
    The rows of data are divided up by subtotaling and obviously ranges have totals row under them, is it possible to modify the code so that those rows are not affected by the formatting? i.e. where there is a blank in J2 then nothing to be done to L2
    Yes, one of the things I like most about the temp formulae approach is the ease with which you can modify requirements... ie

    Please Login or Register  to view this content.
    becomes

    Please Login or Register  to view this content.
    better IMO here to use an embedded IF rather than AND as need not compute the datedif calc for those subtotals rows.

  7. #7
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    So in effect what the code is saying is IF column J cell has nothing then exit, IF it does then check criteria for itself and also adjacent column L cell.
    Please correct me if I'm wrong here
    I am still very inexperienced in VBA, although I can understand what it kind of does by going through the code I'm not entirely sure exactly what and where it happens. Sorry to complicate things!
    Last edited by Fidd$; 10-12-2009 at 06:19 AM.

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Correct.

    I'm pressed for time today so I can't really spend the time explaining as I would like to but in essence the code works along the lines of:

    - Disable Events etc to keep code as efficient as possible

    - Establish "L" range (ie rows to process)

    - Remove formatting from all rows

    - Create a temp column of formulae in the last column on the sheet

    - Get temp formulae to return either "X" (format) or 0 (do not format)

    - Use SpecialCells to quickly identify & format all rows based on formula results ("X" being text and 0 being numbers - you can differentiate)

    - Remove temp formulae column

    - Re-establish Events etc... (ie restore environment)
    If you want to remember you can step through the code using F8 - if you insert a breakpoint in the code (press F9 at an appropriate line) when the code is invoked you can press F8 to step through line by line and follow the impact on the sheet by moving between VBE and Excel
    (the formulae will go in last column to right [whilst they exist])
    Last edited by DonkeyOte; 10-12-2009 at 06:24 AM.

  9. #9
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    ACH!!! Slight niggle. I've put the edited code in and it forces the subtotal rows to 'no fill'. I actually want to be able to (for once!) manually format those rows, only for presentation sakes.
    Or, would it be better to have the code modified to do it for me?

    You've already helped me loads so no worries dude
    Thanks for the explanation too and I also didn't realise you could use F8 to go through line by line on the fly, that will help immensely.

    Tony
    Last edited by Fidd$; 10-12-2009 at 06:34 AM.

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    You will want to format the Subtotal cells as part of the routine (ie at the end).

    If we assume the subtotal rows can be identified by the fact they are the only cells in the range where J is devoid of content then you can perhaps revise the temp formulae to:

    Please Login or Register  to view this content.
    and then modify the subsequent code from:

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    obviously change format(s) as required...

  11. #11
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Ok, apologies for this as I think I didn't explain it very well.
    I inserted the above code in and it does work, provided J doesn't have anything in it i.e. subtotal rows.
    However, I forgot to say that in column J, not all the cells are populated, so in effect the modified code will highlight every cell in L where J is blank and not just the subtotal rows.
    Sorry to be a nuisance Donkey!

    Tony

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    You need then to clearly state how the Subtotal rows can be differentiated from non-subtotal rows given J is not the determining factor in it's own right.

  13. #13
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Ok, column B is the only column that has data unless it's a subtotal row.
    Is it as simple as changing this:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    It's probabaly wrong, so shoot me down if it is

    Tony

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Quote Originally Posted by Fidd$
    Is it as simple as changing this ... to this...
    It is......

  15. #15
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Yup, jus tried it, it's wrong!
    Now all the rows in L are getting highlighted!

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Post a small sample so I can see exactly what your setup is.

  17. #17
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    What's happening now is that if both J and L are blank, it get's highlighted red. If I put something in L it highlights it black (colour of choice for subtotal rows).
    Also, you'll kill me for this, column L is not all filled in, there are blanks in it too, which I have had to fill in with "." for the purposes of analysing.
    Excuse the crap data set, it was given to me via a not so reliable filing company and I've been given the task of trying to get it to a decent state.
    Attached Files Attached Files
    Last edited by Fidd$; 10-12-2009 at 07:11 AM. Reason: added sample file

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    no attachment

    If L contains text that explains why some non subtotal rows are being formatted as subtotals given the DATEDIF calc will (given present arrangement) generate errors, if blank the DATEDIF will generate result > 10/15 years (blank being 0 and 0 being 0 Jan 1900 (1900 date system))

    perhaps:

    Please Login or Register  to view this content.
    Last edited by DonkeyOte; 10-12-2009 at 07:13 AM.

  19. #19
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Ok, I can delete the "." from L and figure another way, no biggie, but then L gets highlighted whether there is a value or not.

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

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Based on the file you added I would suggest you base your initial range detection on A rather than L and adjust remaining code accordingly, eg:

    Please Login or Register  to view this content.
    Last edited by DonkeyOte; 10-12-2009 at 05:49 PM.

  21. #21
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: Conditional formatting based on multiple criteria (needs to be in VBA)

    Donkey Dude, you're a star! This works perfectly now and it is helping me immeasureably with the analysis. I hope I can modify it for similar excercises on the same sheet, just a case of trying to find the right bit of code to modify!
    Your expertise has given me invaluable information regarding Excel, some of which I must admit is absolutely cracking!

    Thank you times a million Donkey!

    Tony

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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