Excel cell reference to it´s previous value (conditional formatting)

3

I have a good handle on Conditional Formatting in general. However, one use-case I have not been able to figure out yet, perhaps someone could give me a cue as to how to achieve it.

I am creating a MarketWatch listing of financial instruments on a sheet. Live data comes in and changes frequently.

My question - if the live price-feed for Stock ABC comes in as 5.00 USD, and then increases, say to 5.50 USD, I would like the cell to turn green. If the subsequent value now turns to 5.40 USD (which is less than the same cell´s "previous" value), I would like the cell to turn red. And so forth on a continuing basis.

The main point (which makes it tricky) is - the cell reference is dynamic - as it references to it´s own previous value), not a static "other cell".

What possibilities might exist to accomplish this?

enter image description here

Ravien

Posted 2016-12-02T13:23:04.587

Reputation: 33

What do you mean by dynamic cell reference? Could you please post some sample data? – Máté Juhász – 2016-12-02T13:28:31.527

thanks for quick response - in other words: if at the moment, the cell value A1 = 5, and then a minute later the cell value changes to 5.5, then I want the cell itself to turn green. The initial value 5 is not hard-coded number into the cell A1, but it comes as a changing value from a financial data provider. so at any moment the value could be any other number as well (the so-called initial value). In other words, the cell would need to be able to know and "remember" its own value that it "was". So that based on this memory it could change its color based on the new value on the fly as needed – Ravien – 2016-12-02T13:32:18.843

Answers

1

You will need to do this manually, as it's not supported by the standard conditional formatting.

You could handle this event: Private Sub Worksheet_Change(ByVal Target As Range)

A way to do this is shown here: https://stackoverflow.com/questions/409434/automatically-execute-an-excel-macro-on-a-cell-change

You would need to compare Target with the current value of the cell. You could then set the colour of the cell using cell.Interior.ColorIndex = ...

--

Two things in response to the comment:

You are right that Change refers only to user-initiated changes. To capture recalculations too, you need to handle the event Calculate as well. Here's an example of that.

Private Sub Worksheet_Calculate()
  Call updateme
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
  Call updateme
End Sub

These two functions call another subroutine updateme whenever either of those events occurs.

Within this function, you need to check if the value has changed, and then decide on the color change. This means keeping track of the previous value in a global variable. You could do this:

Private Sub updateme()
  Set cell = ActiveSheet.Range("A5")
  newval = cell.Value
  If newval > lastval Then
    cell.Interior.ColorIndex = 3
  End If
  If newval < lastval Then
    cell.Interior.ColorIndex = 4
  End If
  If newval = lastval Then
    cell.Interior.ColorIndex = 2
    End If
lastval = newval
End Sub

How do you keep track of that value, lastval, across function calls? You need to initialise the lastval variable as a global variable. To do that, you need to create a new "Module" in the VBA editor, and add this to the module:

Public lastval As Double

Sanjay Manohar

Posted 2016-12-02T13:23:04.587

Reputation: 396

Thanks Sanjay - though unfortunately this would only help half-way as your suggestion addresses "any change for the cell". What I am struggling with, however, is a directional cell change and respective changes as described originally - is there a way to do that? – Ravien – 2016-12-02T16:56:09.090

OK I have updated the answer with a bit more detail. This perhaps is really an answer that belongs on StackOverflow rather than SuperUser. – Sanjay Manohar – 2016-12-02T17:34:33.740

That looks better and it gets a step closer indeed, thank you. However, one remaining concern still - the current solution only works when editing is done directly onto the cell A5 itself (meaning, i.e. enter the value 5 and then press "Enter" on keyboard) and colors start changing and stay correctly until next number is fed to it. In my case though - the cell A5 equals a reference to another cell. Since no "Enter" is pressed, the 'right' colors only flash for a 1/10th of a second (either red or green) and then immediately revert back to default color. How can we overcome that? – Ravien – 2016-12-02T18:29:32.493

Think I solved it now. I just edited out the part where newval = lastval as this somehow seemed to automatically become the prevalent condition from the macro´s perspective when value was derived/references via another cell (eventhough the numbers did differ in sequence). Thank you, Sanjay, for your support and help above! For the main point though, your answer did provide the solution so I am marking your response as an Answer! – Ravien – 2016-12-02T19:32:29.070