在共享数据的单独工作表上绘制多个嵌入式图表

时间:2013-05-16 16:23:25

标签: vba charts plot series

下面的半工作代码将我的“sheet1”中的数据绘制成图表,然后将其移动到自己的工作表并将其嵌入到那里。所有数据都在“sheet1”上。每个图表都需要自己的新页面。每次迭代都使用相同的X轴值,但是单独的Y轴值(可以在下面看到)

我的问题是在第二次迭代中(我想在同一张表中绘制不同的列)。我的下面的代码没有区分每个图表是不同的,并将最后一次迭代绘制两次。我意识到整体编码可能有一种更简洁的方法,但我对VBA很新,这种方式让我可以关注。

我的直觉告诉我每次迭代时将Active.Chart分别更改为Graph1和Graph2但是当我尝试这个时,没有任何不同的事情发生。如何更改语法以告诉VBA在每个迭代中使用新标题等在新页面上启动新图表?

如果有人能指出我正确的方向,我将不胜感激!我知道这很简单,但我很难理解它。

'Plot Forces, Horizontal
Sub PlotResults()
On Error Resume Next
Range("A1").Select 'Prevent ghost plots
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth
ActiveChart.Parent.Name = ("Graph1")

ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "Primary"
ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$A$10:$A$369"
ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$B$20:$B$369"

    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Name = "Secondary"
    ActiveChart.SeriesCollection(2).XValues = "='Sheet1'!$A$10:$A$369"
    ActiveChart.SeriesCollection(2).Values = "='Sheet1'!$C$20:$C$369"

        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(3).Name = "Total"
        ActiveChart.SeriesCollection(3).XValues = "='Sheet1'!$A$10:$A$369"
        ActiveChart.SeriesCollection(3).Values = "='Sheet1'!$D$20:$D$369"
'Titles
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Characters.Text = ("Unbalance Forces, X" & vbCrLf & Model) 'NEED TO FIX THIS
ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Crank Angle, Degrees"
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Force (LBS)"
ActiveChart.Axes(xlCategory).HasMajorGridlines = True
'Formatting
ActiveChart.Axes(xlCategory).HasMinorGridlines = False
ActiveChart.Axes(xlValue).HasMajorGridlines = True
ActiveChart.Axes(xlValue).HasMinorGridlines = False
ActiveChart.HasLegend = True
With ActiveChart.Axes(xlCategory, xlPrimary)
    .MaximumScale = 360
    .MinimumScale = 0
    .MajorUnit = 30
End With
With ActiveChart.Parent 'resize/reposition
    .Height = 525
    .Width = 900
    .Top = 50
    .Left = 100
End With
'Embed chart in own window
ActiveSheet.ChartObjects("Graph2").Activate
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Force, X"






'Plot Moments, Horizontal
Range("A1").Select 'Prevent ghost plots
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterSmooth
ActiveChart.Parent.Name = ("Graph2")

ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "Primary"
ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$A$10:$A$369"
ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$G$20:$G$369"

    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Name = "Secondary"
    ActiveChart.SeriesCollection(2).XValues = "='Sheet1'!$A$10:$A$369"
    ActiveChart.SeriesCollection(2).Values = "='Sheet1'!$H$20:$H$369"

        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(3).Name = "Total"
        ActiveChart.SeriesCollection(3).XValues = "='Sheet1'!$A$10:$A$369"
        ActiveChart.SeriesCollection(3).Values = "='Sheet1'!$I$20:$I$369"
'Titles
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Characters.Text = ("Unbalance Moments, X" & vbCrLf & Model) 'NEED TO FIX THIS
ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Crank Angle, Degrees"
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Moment (FT-LBS)"
ActiveChart.Axes(xlCategory).HasMajorGridlines = True
'Formatting
ActiveChart.Axes(xlCategory).HasMinorGridlines = False
ActiveChart.Axes(xlValue).HasMajorGridlines = True
ActiveChart.Axes(xlValue).HasMinorGridlines = False
ActiveChart.HasLegend = True
With ActiveChart.Axes(xlCategory, xlPrimary)
    .MaximumScale = 360
    .MinimumScale = 0
    .MajorUnit = 30
End With
With ActiveChart.Parent 'resize/reposition
    .Height = 525
    .Width = 900
    .Top = 50
    .Left = 100
End With
'Embed chart in own window
ActiveSheet.ChartObjects("Graph2").Activate
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Moment, X"

End Sub

1 个答案:

答案 0 :(得分:0)

大家好,我偶然发现了解决方案。这很简单。只需在每个绘图迭代结束时添加以下代码(将图表移动到自己的工作表并在调用下一个子例程或结束当前子例程之前嵌入它)

Sheets("Sheet1").Select 'avoid multiple last plots issue

这可确保在绘制图表后,再次选择包含数据的工作表(在我的情况下为sheet1),因此ActiveSheet和ActiveChart命令可以了解正在创建新图表。这就是全部!