0

我下面的半工作代码将我的“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
4

1 回答 1

0

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

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

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

于 2013-05-17T13:33:57.527 回答