尝试创建一个随时间变化数据的图表太迟了?

时间:2016-05-04 02:00:48

标签: vba excel-vba excel

我正在尝试创建一个随时间更改数据的图表。 Y数据相同,但只更改了X数据。我正在使用Application.Wait在X值的每次更改之间允许一段时间(所以我可以用我的眼睛看到它)。但是,当我运行代码时它非常滞后,我不确定我是否正在使用Application.Wait函数,但我希望每次更改时间为0.5秒。有没有办法让它更流畅,以便我可以看到图形随着时间的变化(因为代码循环遍历行)

这是我的代码:

Sub UpdateChart()

Dim ChtObj As ChartObject
Dim counter As Integer
Dim timecount As Double


Set ChtObj = ActiveSheet.ChartObjects.Add(200, 50, 500, 500)

'Creating intial graph
With ChtObj.Chart
    'Chart Type
    .ChartType = xlXYScatterSmooth

    'Datainput
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "Bending moment"
    .SeriesCollection(1).Values = Range("D3:H3")
    .SeriesCollection(1).XValues = Application.Union(Cells(5, 4), Cells(5, 5), Cells(5, 6), Cells(5, 7), Cells(5, 8))
    .HasLegend = False

    'Title
    .HasTitle = True
    .ChartTitle.Text = "Bending moment along pile " & ActiveSheet.Name & " at time 0 seconds"

    'X-Axis
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Bending moment (kN.m)"
    'Y-Axis
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Length along pile (m)"

End With

'Loopingthrough data to be done**
counter = 6
timecount = 0
While (Not IsEmpty(Cells(counter, 4)))

    'Pausing for half a second and recording cumulative time
    timecount = timecount + 0.5
    Application.Wait (Now() + TimeValue("0:00:005"))

    'Updating Chart data
    With ChtObj.Chart
        .SeriesCollection(1).XValues = Application.Union(Cells(counter, 4), Cells(counter, 5), Cells(counter, 6), Cells(counter, 7), Cells(counter, 8))
        .ChartTitle.Text = "Bending moment along pile " & ActiveSheet.Name & " at time " & timecount & " s"
    End With

    'Next row
    counter = counter + 1
Wend
End Sub

1 个答案:

答案 0 :(得分:0)

这似乎解决了我的问题:

Public Function PauseEvent(ByVal Delay As Double)
  Dim dblEndTime As Double
  dblEndTime = Timer + Delay
  Do While Timer < dblEndTime
    DoEvents
  Loop
End Function