VBA to store previous value of continuously updating cell

lb01

New Member
Joined
Feb 12, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
1707740808001.png


Hi,

i have two cells bid q sum (E87) and ask q sum (E88), these values continuously update via means of an API for my trading software.
what im trying to achieve is whenever E87/88 update, i want their previous values stored in F87/88 respectively,

Im a novice with vba and so far have this, which doesnt appear to work:

Private Sub Worksheet_Calculate()
Static oldValueD87 As Variant

' Check if the value of D87 has changed
If Me.Range("D87").Value <> oldValueD87 Then
Application.EnableEvents = False ' Disable events to prevent recursive calls
On Error GoTo CleanUp

' Store the old value of D87 in E87
If Not IsEmpty(oldValueD87) Then
Me.Range("E87").Value = oldValueD87
End If

' Update oldValueD87 with the new value of D87
oldValueD87 = Me.Range("D87").Value

CleanUp:
Application.EnableEvents = True ' Re-enable events
If Err.Number <> 0 Then Err.Clear ' Clear errors if any
End If
End Sub


any help appreciated

Luke
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
If you want to store values from previous runs for comparison sake, the variables must be declared as global variables OUTSIDE of the procedure.
Otherwise, they will have no memory outside of that particular run.

See: VBA Variables - Global Level - Public
 
Upvote 0
Solution
If you want to store values from previous runs for comparison sake, the variables must be declared as global variables OUTSIDE of the procedure.
Otherwise, they will have no memory outside of that particular run.
Fantastic, really appreciate that, its worked as intended! Hero!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top