+ Reply to Thread
Results 1 to 5 of 5

macro to change the color of cells based on their sum

  1. #1
    Forum Contributor
    Join Date
    05-23-2014
    Location
    Fredericia, Denmark
    MS-Off Ver
    Windows Office 2013, Excel 10
    Posts
    139

    macro to change the color of cells based on their sum

    When the sum of B7-D7 is less than cell (I7) I would like to change the shade of B7-D7 to orange to indicate that larger values need to be entered to equal the value of cell I7. And when the sum of these three cells does equal I7 their color should change to green. I recorded two macros to change the colors and I've run them to verify they work. But I've got something off in my simple macro below.

    Thanks for your help. David

    Please Login or Register  to view this content.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: macro to change the color of cells based on their sum

    Why the need for VBA for this? Excel has a Conditional Formatting function that does this really easily

    1. highlight the range you want to apply the conditional formatting to (B7:D7)
    2. on the home tab, styles, select CF
    3. select new rule, select use formula
    4. enter =sum(B7:D7)<I7 Format fill ORANGE

    Repeat 3 & $ with =sum(B7:D7)=I7 Format fill GREEN
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Contributor
    Join Date
    05-23-2014
    Location
    Fredericia, Denmark
    MS-Off Ver
    Windows Office 2013, Excel 10
    Posts
    139

    Re: macro to change the color of cells based on their sum

    Sorry for the tardy response. The reason for not using Conditional Formatting is that these cells need to be Protected with only allowance for entry/erasing of numbers. I tried the VBA code but it did not work for me, unfortunately.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: macro to change the color of cells based on their sum

    CF will not interfere with any protection applied to a cell

  5. #5
    Forum Contributor
    Join Date
    02-24-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: macro to change the color of cells based on their sum

    dgibney,
    If you really need code, then try this.
    However, you didn't define what happens if the sum of the cells is greater than the value of cell I7. So, I suppose you will work that out yourself.
    Please Login or Register  to view this content.
    The code works, but I didn't test it under different conditions (so there may be side-effects). However, it will give you the result you want. But if the cells are protected, you will get run time errors and have to add code to unprotect the sheet while the code runs. Or change the code to a regular Sub. That is why Conditional Formatting would be best.

    Put this code in the Sheet1 module. That way you don't need the "Activate Sheet1" line of code.
    You also don't need the '$' while using vba. The '$' is for formula use only.

    Best of luck to you.
    Last edited by Jim885; 08-07-2014 at 07:57 PM.
    If I helped in any way, please click the star

+ 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. Macro to change cell text color based on darkness of cell fill color
    By cincyshirm61 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-25-2014, 12:11 PM
  2. [SOLVED] change fill color of a range of cells based on color of a cell?
    By DarMelNel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2014, 04:48 PM
  3. VBA / Macro help needed to change the barchart color based on Cell Color
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2012, 09:15 AM
  4. Can cells change color based on formula/data?
    By Bryy2001 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-14-2012, 12:43 PM
  5. Change the color of cells based on values
    By Weasel in forum Excel General
    Replies: 1
    Last Post: 05-01-2007, 12:58 PM

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