+ Reply to Thread
Results 1 to 9 of 9

Request for simple conditional formatting code in VBA

  1. #1
    Registered User
    Join Date
    10-08-2021
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Request for simple conditional formatting code in VBA

    Hi,

    I am looking for a simple block of code that can change the background colour from a cell depending on the value in that cell.

    Let me explain:

    I want each cell in a range from A1 to A20 on sheet("Worksheet1") change in background colour depending on it's value.

    If the value is between 0 and 10 then the background colour needs to change to RGB(255,100,230)
    If the value is between 10 and 20 then the background colour needs to change to RGB(100,100,050)
    Etc.

    The formatting will need to run continuously in the background so that there's no need for an "Update"-button.

    It must be straight forward process but after hours of surfing the net I gave up. Hence my question here.

    I do not want to use the Conditional Formatting formulas as available in Excel as I want to go for a specialized colour scheme.
    Thanks for your help!

    Luke

  2. #2
    Registered User
    Join Date
    10-08-2021
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Re: Request for simple conditional formatting code in VBA

    Further to my original message I would like to add the following:

    I am developing a spreadsheet that deals with local weather data.
    I want to introduce conditional formatting to each data cell that is in line with the official clourscheme from the Bureau Of meteorology.
    So, for the temperature scale (Celsius) the BoM uses following RGB colour schemes:

    45+ 201,32,38
    42-45 255,85,39
    39-42 244,126,32
    36-39 253,187,34
    33-36 254,217,84
    30-33 249,241,137
    27-30 220,237,200
    24-27 209,226,190
    21-24 184,209,164
    18-21 158,192,138
    15-18 133,175,112
    12-15 107,158,86
    9-12 76,155,163
    6-9 104,174,175
    3-6 138,187,191
    0-3 92,212,224
    -3-0 133,233,232
    -6-3 174,234,240
    < -6 214,244,247
    no data 211,211,211

    So, the moment I put in a value for temperature and after I hit "Enter" then I would expect the VBA code to run and change the background colour of the cell to the corresponding RGB-value.
    I would like to use the "CASE"-function as this is neat and easy to read.
    I need to do this in VBA as it needs to be hard coded. The Conditional Formatting in Excel is too cumbersome, doesn't allow that many parameters and is easily corrupted while applying the copy and paste function.
    Any suggestions?
    Thanks for your help in advance!
    Cheers,

    Luke

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Request for simple conditional formatting code in VBA

    Hi Luke,

    Couple of questions:

    • Don't the ranges overlap i.e. what RGB would you use if the temperature was 39°C?
    • Is the code to run on any cell change or a specific range?

    Thanks,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Registered User
    Join Date
    10-08-2021
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Re: Request for simple conditional formatting code in VBA

    Hi Robert,

    Thanks for your time.
    Yes, there is some overlap. But, basically what it is, is that 38.9C would still be the end of the previous colour and 39.0C would be the start of the new colour.
    Temperatures are expressed to 1 decimal accuracy.

    To answer your second question:
    The table will contain all historical records, whether it's for minimum temperature or maximum rainfall in a period.
    So, whenever new data is entered in a data table then Excel should check if this new data breaks a record or not. So, yes, once day when the new data is added then the program should update the table and change the background when required.

    I hope this will make sense.
    Thanks for your interest and time.
    Cheers,

    Luke

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Request for simple conditional formatting code in VBA

    The bands may need tweaking but this event macro on the sheet in question will do the job:

    Please Login or Register  to view this content.
    Regards,

    Robert

  6. #6
    Registered User
    Join Date
    10-08-2021
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Re: Request for simple conditional formatting code in VBA

    Thanks Robert!
    That sounds like a great solution and easy to follow.
    I will try it out tomorrow morning and will definitely get back to you.
    Thank you very much for all your help (And all that typing)!
    Kind regards,

    Luke

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Request for simple conditional formatting code in VBA

    Thanks Luke.

    I think the top range should actually be:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    10-08-2021
    Location
    Melbourne, Australia
    MS-Off Ver
    2016
    Posts
    14

    Re: Request for simple conditional formatting code in VBA

    Dear Robert,

    I ran your code in my spreadsheet and I'm very pleased to let you know that everything works a treat!
    It behaves just as expected and I didn't had any bugs to deal with at all.
    That is really a fine piece of code and I will keep it in my code snippets library.
    Thank you very much for your time and effort! I really appreciate it.
    I will close off this topic now and will change the status to "Solved".
    Have a great day and thank you very much helping me out!
    Kind regards,

    Luke

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Request for simple conditional formatting code in VBA

    Hi Luke,

    You're welcome and I'm glad it all worked out. Thanks for marking the thread as solved and the rep

    Regards,

    Robert

+ 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 steps request
    By mso3 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-12-2015, 08:03 PM
  2. Simple VB code request...I think....
    By docfrye in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2014, 10:00 AM
  3. [SOLVED] Excel 2007: Conditional Formatting Request for Help!
    By wilsoncj322 in forum Excel General
    Replies: 19
    Last Post: 07-30-2012, 01:39 PM
  4. [SOLVED] IF THEN or Conditional Formatting question help request
    By ac14461 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-25-2012, 09:25 PM
  5. IF THEN or Conditional Formatting question help request
    By ac14461 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-25-2012, 12:14 PM
  6. [SOLVED] Opinion Request: Conditional Formatting
    By OcMac in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2012, 04:07 PM
  7. [SOLVED] CF in VB Code - Simple Request
    By Kevin Baker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2005, 08:06 AM

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