Friday, 25 March 2011

Previous value in a cell in Excel

I have an excel sheet with RTD entries in it. I wanted the difference between the latest value and the previous value, but couldn't find anywhere on a quick search for the previous value
Here's a quick sample function that does that and caches the previous value. I'd have suspicions about its thread safety though.


'Function to get the difference between the previous value in a cell and the current value
Public Function LastDifference(cell_reference As String, value as Double)
Static d
If Not IsObject(d) Then Set d = CreateObject("Scripting.Dictionary")
If d.exists(cell_reference) Then
  lastValue = d.Item(cell_reference)
Else
 lastValue = 0
End If


d.Item(cell_reference) = value
LastDifference = value - lastValue
End Function