具有延迟的Excel图表的dynamic更新

我使用Excel VBA从仪器读取数据行。 我想在Excel Active Chart上dynamic地绘制数据,在读取数据之后立即绘制数据。 我需要每隔5秒等待并读取一次数据,同时通过VBA Application.Wait命令或Kernel32 Sleep命令“睡眠”。 在任何一种情况下,活动图表都不会被更新。 完整的情节只有在最后一次“睡眠”之后才会出现。 任何build议将不胜感激。

这是简化的代码

Sub New_Data(indx) Dim x As Integer While True x = Read_Instrument(1) y = Read_Instrument(2) Cells(indx, 1) = x Cells(indx, 2) = y ActiveSheet.ChartObjects.Item(1).Activate ActiveChart.FullSeriesCollection(1).XValues = "=Sheet1!$A$1:$A$" & indx ActiveChart.FullSeriesCollection(1).Values = "=Sheet1!$B$1:$B$" & indx indx = indx + 1 Sleep 5000 'Use the KERNEL32 Sleep function for 5000 milliseconds Wend End Sub 

WaitSleep将保持VBA运行,所以屏幕不会更新。 尝试使用Application.OnTime ,沿着这些线(在一个标准的代码模块,即模块1)。

 Sub refreshMyChart() Dim indx as long: indx = sheet1.Cells(Rows.Count, 1).End(xlUp).offset(1) Cells(indx, 1) = Read_Instrument(1) Cells(indx, 2) = Read_Instrument(2) With Sheet1.ChartObjects(1).FullSeriesCollection(1) .XValues = "=Sheet1!$A$1:$A$" & indx .Values = "=Sheet1!$B$1:$B$" & indx End With ''''' Now Schedule the same routine for 5 seconds later'''''''''' Application.OnTime Now + TimeSerial(0,0,5), "refreshMyChart" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' End Sub 

ps:请注意,这些数据不能无限增长。您应该定义一些方法来停止或删除旧的行,以保持显示的数据行的数量合理。

优秀! 非常感谢你。 我不知道.OnTime构造Application.OnTime Now + TimeSerial(0,0,5),“refreshMyChart”和VBA允许recursion例程的事实。 至于结束循环,那里有代码,我没有发布,因为它是不相关的问题。 这里是如果Int(current_index / nmeasure)* nmeasure> finalval那么MsgBox(“测量结束”)Exit Sub End If

再次感谢。