Excel calculate increase in value every 200ms

[亡魂溺海] 提交于 2019-12-04 05:10:40

问题


I have this worksheet which gets data from API and its refreshes itself every 200 milliseconds. I want to calculate the change in value which is constantly increasing every 200 ms. For example Cell B2 has a value of 4 after 200 ms its changes to 7 then to 16 then to 26 etc, it just keeps adding value into it. All I want is to subtract the old value from the latest value to get the change for example 7-4=3 or 16-7=9 or 26-16=10.

I have added an image for clarification. This shows how I'm getting a data from software.

And one more image:


回答1:


I suggest VBA solution, based on worksheet change event handling. Open VBA Project and put the below code into the target worksheet in Microsoft Excel Objects section:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    ' add reference to Microsoft Scripting Runtime via Menu - Tools - References

    Const Scope = "C2:C5" ' monitoring area
    Const DX = 1 ' horizontal result offset
    Const DY = 0 ' vertical result offset
    Const Buf = 0 ' FIFO buffer size

    Static oData(0 To Buf) As New Dictionary
    Static oIndex As New Dictionary
    Dim rCells As Range
    Dim oCell
    Dim i As Long

    Set rCells = Application.Intersect(Target, Target.Parent.Range(Scope))
    If Not rCells Is Nothing Then
        For Each oCell In rCells
            With oCell
                i = oIndex(.Address)
                .Offset(DY, DX).Value = .Value - oData(i)(.Address)
                oData(i)(.Address) = .Value
                i = i + 1
                If i > Buf Then i = 0
                oIndex(.Address) = i
            End With
        Next
    End If

End Sub

I added some comments for constants. Set the range which change to be monitored in Scope, the offsets where the resulting delta will be output in DX and DY, and as a bonus that algorithm supports computing delta not only between last and previous numbers, but also between any number of frames for each target cell via buffer organized as array of dictionaries, so set the size of the buffer in Buf, if you do not want to use the buffer then just leave 0 size, e. g. the value of 3 will compute delta between the last value and the one delayed by 800 ms for your case.

UPDATE

There is slightly simplified version of the code as requested in comment, put the below code into the target worksheet:

Private Sub Worksheet_Change(ByVal Target As Range)

    Const Scope = "C2:C5" ' monitoring area

    Static oData As New Dictionary
    Dim rCells As Range
    Dim oCell
    Dim dDelta

    Set rCells = Application.Intersect(Target, Target.Parent.Range(Scope))
    If Not rCells Is Nothing Then
        For Each oCell In rCells
            With oCell
                dDelta = .Value - oData(.Address)
                If dDelta <> 0 Then
                    .Offset(0, 1).Value = dDelta
                    oData(.Address) = .Value
                End If
            End With
        Next
    End If

End Sub



回答2:


First, enable Iterative Calculations in Excel by going to File -> Options -> Formulas and then checking the box next to "Enable iterative calculation".

You need to define the following cells:

cell B1     0 (set to 1 to reset)
cell B2     =IF($B$1 = 1,, $B$2 + 1)

Use the following formula and fill down from B9 for as many changes as you would like to see (This formula assumes you have maximum iterations set to 100):

cell B9 =IF($B$1 = 1,"", IF($B$2 / 100 = $A9, $B$5, B9))

I will try to show an example here. If your cell that automatically updates is B5, then the changes will be tracked in B9 and below as the cell is refreshed. It may not be exactly what you are looking for, but I think it is close.

     A      B
1    reset  0
2    count  500
3       
4       
5    price  9
6       
7       
8    ID     price
9    1      11
10   2      12
11   3      13
12   4      12
13   5      9


来源:https://stackoverflow.com/questions/40369384/excel-calculate-increase-in-value-every-200ms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!