2017-07-25 46 views
1

我正在從多個電子表格中獲取數據並將它們繪製在圖表上,分別用於各個電子表格。我希望Spreadsheet1的數據也可以在Spreadsheet1上繪製圖表。目前,我的代碼繪製了最後一張紙上的所有圖形,所以圖1,2,3等圖形都繪製在最後一張紙上。我不確定如何解決這個問題,因爲我是VBA新手。我記錄了一個宏以獲取代碼來繪製數據。如何在相應的圖紙上繪製圖表?

這裏是我的繪圖代碼:

For j = 1 To size 
'creates chart 

ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select 
ActiveSheet.Shapes("Chart 1").IncrementLeft 696.75 
ActiveSheet.Shapes("Chart 1").IncrementTop -81.75 
ActiveSheet.Shapes("Chart 1").ScaleWidth 1.3333333333, msoFalse, _ 
    msoScaleFromTopLeft 
ActiveSheet.Shapes("Chart 1").ScaleHeight 1.6909722222, msoFalse, _ 
    msoScaleFromTopLeft 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(1).Name = "=""Length and Depth Data""" 
ActiveChart.FullSeriesCollection(1).XValues = Worksheets("Case " & overview(j, 1)).Range("$R$10:$R$6000") 
ActiveChart.FullSeriesCollection(1).Values = Worksheets("Case " & overview(j, 1)).Range("$S$10:$S$6000") 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(2).Name = "=""B31G MAOP""" 
ActiveChart.FullSeriesCollection(2).XValues = Worksheets("Case " & overview(j, 1)).Range("$C$10:$C$159") 
ActiveChart.FullSeriesCollection(2).Values = Worksheets("Case " & overview(j, 1)).Range("$I$10:$I$159") 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(3).Name = "=""B31G 1.25SF""" 
ActiveChart.FullSeriesCollection(3).XValues = Worksheets("Case " & overview(j, 1)).Range("$C$10:$C$159") 
ActiveChart.FullSeriesCollection(3).Values = Worksheets("Case " & overview(j, 1)).Range("$J$10:$J$159") 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(4).Name = "=""B31G 1.39SF""" 
ActiveChart.FullSeriesCollection(4).XValues = Worksheets("Case " & overview(j, 1)).Range("$C$10:$C$159") 
ActiveChart.FullSeriesCollection(4).Values = Worksheets("Case " & overview(j, 1)).Range("$P$10:$P$159") 
ActiveWindow.SmallScroll Down:=-126 
ActiveWindow.ScrollColumn = 3 
ActiveWindow.ScrollColumn = 4 
ActiveWindow.ScrollColumn = 5 
ActiveWindow.ScrollColumn = 6 
ActiveWindow.ScrollColumn = 7 
ActiveWindow.ScrollColumn = 8 
ActiveWindow.SmallScroll Down:=6 
Range("W32").Select 
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select 
ActiveSheet.Shapes("Chart 2").IncrementLeft 311.25 
ActiveSheet.Shapes("Chart 2").IncrementTop 213 
ActiveWindow.SmallScroll Down:=18 
Range("AD46:AD47").Select 
ActiveSheet.ChartObjects("Chart 2").Activate 
ActiveSheet.ChartObjects("Chart 2").Activate 
ActiveSheet.Shapes("Chart 2").ScaleWidth 1.3145833333, msoFalse, _ 
    msoScaleFromTopLeft 
ActiveSheet.Shapes("Chart 2").ScaleHeight 1.4930555556, msoFalse, _ 
    msoScaleFromTopLeft 
ActiveSheet.ChartObjects("Chart 2").Activate 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
Application.CutCopyMode = False 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(1).Name = "=""Length and Depth Data""" 
ActiveChart.FullSeriesCollection(1).XValues = Worksheets("Case " & overview(j, 1)).Range("$R$10:$R$6000") 
ActiveChart.FullSeriesCollection(1).Values = Worksheets("Case " & overview(j, 1)).Range("$S$10:$S$6000") 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(2).Name = "=""MB31G MAOP""" 
ActiveChart.FullSeriesCollection(2).XValues = Worksheets("Case " & overview(j, 1)).Range("$C$10:$C$159") 
ActiveChart.FullSeriesCollection(2).Values = Worksheets("Case " & overview(j, 1)).Range("$N$10:$N$159") 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(3).Name = "=""MB31G 1.25SF""" 
ActiveChart.FullSeriesCollection(3).XValues = Worksheets("Case " & overview(j, 1)).Range("$C$10:$C$159") 
ActiveChart.FullSeriesCollection(3).Values = Worksheets("Case " & overview(j, 1)).Range("$O$10:$O$159") 
ActiveChart.SeriesCollection.NewSeries 
ActiveChart.FullSeriesCollection(4).Name = "=""B31G 1.39SF""" 
ActiveChart.FullSeriesCollection(4).XValues = Worksheets("Case " & overview(j, 1)).Range("$C$10:$C$159") 
ActiveChart.FullSeriesCollection(4).Values = Worksheets("Case " & overview(j, 1)).Range("$P$10:$P$159") 
ActiveWindow.SmallScroll Down:=-117 
ActiveSheet.ChartObjects("Chart 1").Activate 
ActiveChart.PlotArea.Select 
ActiveChart.SetElement (msoElementChartTitleAboveChart) 
ActiveWindow.SmallScroll Down:=9 
ActiveChart.ChartTitle.Text = "B31G Burst Curve" 
Selection.Format.TextFrame2.TextRange.Characters.Text = "B31G Burst Curve" 
With Selection.Format.TextFrame2.TextRange.Characters(1, 16).ParagraphFormat 
    .TextDirection = msoTextDirectionLeftToRight 
    .Alignment = msoAlignCenter 
End With 
With Selection.Format.TextFrame2.TextRange.Characters(1, 16).Font 
    .BaselineOffset = 0 
    .Bold = msoFalse 
    .NameComplexScript = "+mn-cs" 
    .NameFarEast = "+mn-ea" 
    .Fill.Visible = msoTrue 
    .Fill.ForeColor.RGB = RGB(89, 89, 89) 
    .Fill.Transparency = 0 
    .Fill.Solid 
    .size = 14 
    .Italic = msoFalse 
    .Kerning = 12 
    .Name = "+mn-lt" 
    .UnderlineStyle = msoNoUnderline 
    .Spacing = 0 
    .Strike = msoNoStrike 
End With 
ActiveSheet.ChartObjects("Chart 2").Activate 
ActiveChart.Axes(xlValue).MajorGridlines.Select 
ActiveChart.SetElement (msoElementChartTitleAboveChart) 
ActiveWindow.SmallScroll Down:=-12 
ActiveChart.ChartTitle.Text = "B31G Burst Curve" 
Selection.Format.TextFrame2.TextRange.Characters.Text = "B31G Burst Curve" 
With Selection.Format.TextFrame2.TextRange.Characters(1, 16).ParagraphFormat 
    .TextDirection = msoTextDirectionLeftToRight 
    .Alignment = msoAlignCenter 
End With 
With Selection.Format.TextFrame2.TextRange.Characters(1, 4).Font 
    .BaselineOffset = 0 
    .Bold = msoFalse 
    .NameComplexScript = "+mn-cs" 
    .NameFarEast = "+mn-ea" 
    .Fill.Visible = msoTrue 
    .Fill.ForeColor.RGB = RGB(89, 89, 89) 
    .Fill.Transparency = 0 
    .Fill.Solid 
    .size = 14 
    .Italic = msoFalse 
    .Kerning = 12 
    .Name = "+mn-lt" 
    .UnderlineStyle = msoNoUnderline 
    .Spacing = 0 
    .Strike = msoNoStrike 
End With 
With Selection.Format.TextFrame2.TextRange.Characters(5, 12).Font 
    .BaselineOffset = 0 
    .Bold = msoFalse 
    .NameComplexScript = "+mn-cs" 
    .NameFarEast = "+mn-ea" 
    .Fill.Visible = msoTrue 
    .Fill.ForeColor.RGB = RGB(89, 89, 89) 
    .Fill.Transparency = 0 
    .Fill.Solid 
    .size = 14 
    .Italic = msoFalse 
    .Kerning = 12 
    .Name = "+mn-lt" 
    .UnderlineStyle = msoNoUnderline 
    .Spacing = 0 
    .Strike = msoNoStrike 
End With 
ActiveSheet.ChartObjects("Chart 1").Activate 
ActiveChart.ChartTitle.Select 
Application.CommandBars("Format Object").Visible = False 
ActiveChart.ChartTitle.Text = "MB31G Burst Curve" 
Selection.Format.TextFrame2.TextRange.Characters.Text = "MB31G Burst Curve" 
With Selection.Format.TextFrame2.TextRange.Characters(1, 17).ParagraphFormat 
    .TextDirection = msoTextDirectionLeftToRight 
    .Alignment = msoAlignCenter 
End With 
With Selection.Format.TextFrame2.TextRange.Characters(1, 17).Font 
    .BaselineOffset = 0 
    .Bold = msoFalse 
    .NameComplexScript = "+mn-cs" 
    .NameFarEast = "+mn-ea" 
    .Fill.Visible = msoTrue 
    .Fill.ForeColor.RGB = RGB(89, 89, 89) 
    .Fill.Transparency = 0 
    .Fill.Solid 
    .size = 14 
    .Italic = msoFalse 
    .Kerning = 12 
    .Name = "+mn-lt" 
    .UnderlineStyle = msoNoUnderline 
    .Spacing = 0 
    .Strike = msoNoStrike 
End With 

Next j 
+0

遍歷所有片您將要創建圖表隨着各個片材(完帶)。數據需要指定爲表格(「NAME」)。範圍(「ERB」)。 – Cyril

回答

1

我會做這完全不同的方式。但是,速戰速決,你的資料將被添加此到代碼的牆開始你的for循環開始只是右後:

For j = 1 To size 
    'creates chart 

     Worksheets("Case " & overview(j, 1)).Activate 

    'Rest of the code would be the same 
    '... 

Next j 

你應該閱讀這個問題,它的答案:

How to avoid from selecting and activating in VBA?

0

您應用ActiveSheet上的所有說明。您可以選擇Sheets(1).ActivateSheets("sheet_name").Activate的特定工作表。

也可以通過

For Each sht In ActiveWorkbook.Sheets 
    If sht.Name Like "..." Then ... 
Next sht