+ Reply to Thread
Results 1 to 16 of 16

VBA Conditional formatting (Less than / Greater than) for numerous ranges

  1. #1
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hi!

    With a help of samples and Google I've created this VBA code (on Worksheet_Change)

    Please Login or Register  to view this content.
    (Shows how little I know about VBA, really)

    Thing is my data is loads of numbers in range of A2:AE5350 (and growing daily (row-wise)
    but each column has a different Less than/Greater than targets.

    Above code works fine for column A, where criteria is as per code, but what do I do to replicate
    this code so it applies same conditional formatting for rest of the columns, with different criteria for each?

    Do I just simply duplicate :

    Please Login or Register  to view this content.
    For/Next bit and change the Range and Values and so on for each column up till AE?
    Also I think I need to increase the row count from current 5350 (number of actual records) to something like 10000 so
    that it lasts for some time, without changing the code again, right?

    Please help.

    Regards,
    Janis

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Ramzess,

    Welcome to the Forum!

    Will each column have the same conditional test applied to the cells?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hi Leith,

    Thank you for welcoming.
    No, each column will have different conditional test. Values will differ.

    Regards,
    Janis

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Janis,

    Can you post what the conditions are for each column? Little bit difficult to write the code otherwise.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Janis,

    Here is an example where you can expand your conditions for each column. This code can be condensed more if you are applying the same Font Colors to each column.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Wow, thank you Leith. This is much more complicated than I thought it will be. You done too much work. I appreciate it very much.

    What does variable "r" stand for? I cannot see it defined anywhere and code gives an error because of it.

    Kind regards,
    Janis

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Janis,

    The variable 'r' should be dimensioned as Long. It is the row number of the data.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Strange, I added
    Please Login or Register  to view this content.
    but when executed, code still gives me an error: "Compile error: Type mismatch" in
    Please Login or Register  to view this content.
    as for colour coding, yes, I will have only three colours - red, green and black across the whole data range.

    Regards,
    Janis

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Janis,

    Can you post a copy of your workbook for review?

    B]How To Post Your Workbook[/B]
    1. At the bottom right of the Reply window, Click the button Go Advanced
    2. At the top of the Your Message Window, in the first row of icons, Click the Paperclip icon.
    3. Click the Add Files button at the top right in the dialog box. This displays the File Manager dialog.
    4. Click the Select Files button at the bottom middle of the dialog.
    5. In the new window Find the file you want to upload, select it, and Click Open.
    6. You will now be back in the File Manager dialog. Click the bottom Middle button Upload File.
    7. Wait until the file has completely uploaded before you exit the File Manager dialog.

    File Manger Picture

  10. #10
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Here you go Leith. I am currently at work and don't have the original file at hand as it is not work related, but I created a good example of what it looks and contains.
    Hope it helps.

    Regards,
    Janis
    Attached Files Attached Files

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Janis,

    Having the workbook makes life so much easier. I changed the macro to work with your data. It has been simplified by creating a second macro to set the font color based on the low and high conditions. The code below has been added to the attached workbook.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Massive, massive thank you, Leith. You helped alot.

    Regards,
    Janis

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hello Janis,

    You're welcome. Glad I help out.

  14. #14
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Hi Leith,

    Can you please tell me how do I make this code to run through my existing data, as is seems it only applies criteria to new data added?

    Thank you.

  15. #15
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    Also I think I forgot about something hence giving you misleading information - my data range actually is B2:AE2 not A2:AE2
    and in this data range there are columns I don't need conditional formatting criteria to be applied. I'm sorry I didn't tell you this early.

    Anything you can do about it now?
    Thank you.
    Janis

  16. #16
    Registered User
    Join Date
    11-04-2011
    Location
    Southampton, UK
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: VBA Conditional formatting (Less than / Greater than) for numerous ranges

    copy of actual spreadsheet (part of it)
    Attached Files Attached Files

+ 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. Excel 2007 : Conditional formatting-Value is greater than
    By satishtote123 in forum Excel General
    Replies: 1
    Last Post: 06-11-2011, 12:17 PM
  2. Conditional Formatting and Greater than BUT less than
    By dagindi in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-07-2010, 03:08 PM
  3. Conditional Formatting greater than 100%
    By henro8 in forum Excel General
    Replies: 5
    Last Post: 10-08-2008, 05:18 PM
  4. Conditional Formatting if greater than zero
    By jkpha in forum Excel General
    Replies: 2
    Last Post: 10-31-2007, 01:50 PM
  5. Conditional formatting for the greater of 7 cells
    By guest123 in forum Excel General
    Replies: 8
    Last Post: 01-15-2007, 01:39 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