2013-05-16 10 views
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 

답변

0

좋아, 모두가 해결책을 찾았습니다. 그것은 매우 간단합니다. 그냥 (현재의 서브 루틴을 자신의 시트에 차트를 이동하고 다음 서브 루틴을 호출하기 전에 아직 삽입 또는 종료 후) 각 플롯 반복의 끝에 다음 코드를 추가

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

이 후 보장하여 차트가 플롯되면 데이터가 포함 된 시트 (제 경우, sheet1)가 다시 선택되므로 ActiveSheet 및 ActiveChart 명령은 새 차트가 작성되고 있음을 인식합니다. 그게 다야!

관련 문제