2013-05-16 47 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命令知道正在創建一個新的圖表。就這些!