+ Reply to Thread
Results 1 to 4 of 4

color code cells based on changes to cell value

  1. #1
    Registered User
    Join Date
    01-13-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    11

    color code cells based on changes to cell value

    Hello,

    I have a range of cells (e.g. A1:A10) with formulas whose values keep changing if the calculation mode is automatic, e.g. stock prices.
    How do I write VBA code to color the cell green if the value goes up, red if the value goes down, and yellow if the value remains the same?

    Thanks.

  2. #2
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: color code cells based on changes to cell value

    You'd need to store the previous value - the most logical place would be in an adjacent column. Then you wouldn't need code, just conditional formatting. Assuming you are running code to update the values, the same code could be used to copy the current values to the adjacent column before refreshing.
    CC


    If you feel really indebted please consider a donation to charity. My preferred charity is ActionAid but there are plenty of worthy alternatives.

  3. #3
    Registered User
    Join Date
    01-13-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: color code cells based on changes to cell value

    Hi CC

    I don't have access to the code that updates the values. Cell A1 could be =STOCKPRICE("MSFT"), Cell A2 could be =STOCKPRICE("AAPL"), etc. The STOCKPRICE function updates the values in real-time and is provided by a third-party add-in, so I don't have control over the "refresh" in order to store and update the previous value before refreshing...

  4. #4
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: color code cells based on changes to cell value

    Regardless of your control over the process, you can't compare one value with another which doesn't exist (because it's been overwritten).

    I think that means you would need two copies of each value so you could retain a current version which wouldn't get overwritten, and a previous version to do your actual comparison. The approach is - on sheet change, copy the new version to a 'current' copy after copying the 'current' to a 'previous' then conditional formatting would compare the live with the 'previous' - like so:

    Start:
    Cells: A1 | B1 | C1
    =stockprice("MSFT") | <empty> | <empty>

    code:
    on worksheet change
    disable events
    copy B1 value to C1 then copy A1 value to B1
    enable events

    Refresh one:
    =stockprice("MSFT") | <Value of A1 at time 1> | <empty>

    Refresh 2
    =stockprice("MSFT") | <Value of A1 at time 2> | <Value of A1 at time 1>

    Apply conditional formats

    So B1 would always = A1, except it doesn't get overwritten when A1 is updated

  5. #5
    Registered User
    Join Date
    01-13-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: color code cells based on changes to cell value

    Hi CC,

    Thanks for your help. I tried using the worksheet_change event but that doesn't trigger on update, I've put it under worksheet_calculate and that works.
    I've also used a global array to store the prices and coloring the cells from VBA instead of putting in adjacent cells and using conditional formatting. I suspect that using a global array would be faster than cell access, but I've not verified that.

    Thanks once again.

+ 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