2013-05-16 25 views
0

下面我的半工作代码将数据从我的“工作表1”绘制到图表中,然后将其移动到其自己的工作表并将其嵌入到那里。所有数据都在“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 

回答

0

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

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

这确保了后你图表被绘制,带有数据的表格(在我的例子中是sheet1)被再次选中,因此你的ActiveSheet和ActiveChart命令知道正在创建一个新的图表。就这些!