+ Reply to Thread
Results 1 to 11 of 11

VBA or Formula to set cell colour based on an IF condition

  1. #1
    Registered User
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    MS365 Version 2301
    Posts
    68

    VBA or Formula to set cell colour based on an IF condition

    Hi I'm looking for a way to dot he following without using conditional formatting.


    if cell K3 to W3 value is equal to or less than cell J3+X3 then set the background colour of cell K3 to W3 to Red

    Cell K3 to W3 is just a range and the formula would need to work from the individual cells within that range and not sum the cells.
    I have done this using conditional formatting but it's very hard to create a different new formula for each cell i need checking so i was hoping i could do this either directly ito the sheet using a fomrula or using VBA.

    Regards

    Lee
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: VBA or Formula to set cell colour based on an IF condition

    I need more info.
    if cell K3 to W3 value is equal to or less
    do you mean each cell value or total value (sum) of cell K3 to W3

    What about blank cell in K3 to W3, should it be red too?

  3. #3
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: VBA or Formula to set cell colour based on an IF condition

    I still think conditional formatting is the better of the two ways, so I've included both in your example.

    Conditional formatting. Select the whole range that you want red then enter this formula.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    VBA: In the sheet code page.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    David
    (*) Reputation points appreciated.

  4. #4
    Registered User
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    MS365 Version 2301
    Posts
    68

    Re: VBA or Formula to set cell colour based on an IF condition

    Sorry it's the individual cell value so,

    if K3 is equal to or less than J3+X3 then cell K3 turns RED.

    on the next row it would change to

    if K4 is equal to or less than J4+X3 then cell K4 turns RED.

    Also it would need to work accross columns such as below

    if L3 is equal to or less than J3+X3 then cell L3 turns RED.

  5. #5
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: VBA or Formula to set cell colour based on an IF condition

    Try this:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    MS365 Version 2301
    Posts
    68

    Re: VBA or Formula to set cell colour based on an IF condition

    Sorry just noticed i have the formula wrong, it should be

    if K4 is equal to or less than J1+X3 then cell K4 turns RED and so on...

    J1 will only change accross when moving cells so K6 it would be M1

  7. #7
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: VBA or Formula to set cell colour based on an IF condition

    if K4 is equal to or less than J1+X3 then cell K4 turns RED and so on...
    more example, please.

    J1 will only change accross when moving cells so K6 it would be M1
    why K6 would be M1?
    and J1 is not numeric.

    Could you attach more sample? showing the result you expect

  8. #8
    Registered User
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    MS365 Version 2301
    Posts
    68

    Re: VBA or Formula to set cell colour based on an IF condition

    Please see attached.
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: VBA or Formula to set cell colour based on an IF condition

    Try this:
    In cell Q4, why is it red?
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    12-08-2015
    Location
    London, England
    MS-Off Ver
    MS365 Version 2301
    Posts
    68

    Re: VBA or Formula to set cell colour based on an IF condition

    Thank you the code works really well!

    I have tried to adapt it for the master file i have but im pretty new at this and can't seem to get it to work. Could you please adapt your code for me to work on the new file?

    Please note on the new file K3 now starts at K4 and X3 is now AQ4

    Regards

    Lee
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: VBA or Formula to set cell colour based on an IF condition

    Ok, try this
    Please Login or Register  to view this content.

+ 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. Formula based on cell colour
    By aas103 in forum Excel General
    Replies: 2
    Last Post: 06-26-2014, 05:21 AM
  2. [SOLVED] Formula based on the colour of the cell (counting)
    By renix in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 09-24-2013, 10:49 AM
  3. [SOLVED] If formula based on cell colour
    By TwistedGhost in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-05-2013, 03:06 PM
  4. Change Column Colour Based on Condition
    By redo23 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-30-2013, 06:42 AM
  5. [SOLVED] Including a cell in a formula based on its colour
    By JJ Dimery in forum Excel General
    Replies: 2
    Last Post: 08-23-2012, 05:41 AM
  6. VBA if formula based on cell colour
    By Sharp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2008, 02:48 PM
  7. colour cells based on condition
    By baz0912 in forum Excel General
    Replies: 6
    Last Post: 07-01-2008, 11:37 AM

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