相同图表更新的动态输入

时间:2016-05-04 01:05:35

标签: vba excel-vba excel

我正在尝试创建一个图表,随着时间的推移自动更新其值。数据的Y值是恒定的,而X值随时间变化。我的数据如下:

               Y data = 1    1.5     2    2.5      3
At time = 0    X data = 10   10     10    10       10
At time = 0.5  X data = 20   20     20    20       20
At time = 1.0  X data = 30   30     30    30       30
At time = 1.5  X data = 40   40     40    40       40

*注意我的X数据不符合要求,它们分为5列,如上所示但不是连续的列。

这是我到目前为止所达成的目标

Sub UpdateChart()
    Dim ChtObj As ChartObject
    Dim BM1 As Range, BM2 As Range, BM3 As Range, BM4 As Range, BM5 As Range

Set ChtObj = ActiveSheet.ChartObjects.Add(200, 150, 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")
    ''How to do this .SeriesCollection(1).XValues = Range((Cells(5, 4), Cells(5, 6), Cells(5, 8), Cells(5, 10), Cells(5, 12))
    .HasLegend = False

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

    '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**

End Sub

如何使用Cells()函数输入X数据,以便将其合并到循环中? 非常感谢您的帮助!

我的X数据不是连续的

1 个答案:

答案 0 :(得分:0)

要从活动工作表中添加不连续范围,请使用

.SeriesCollection(1).XValues = Application.Union(Cells(5, 4), Cells(5, 6), Cells(5, 8), Cells(5, 10), Cells(5, 12))