如何在相应的工作表上绘制图表?

时间:2017-07-25 15:26:29

标签: excel vba excel-vba plot charts

我正在从多个电子表格中获取数据并在图表上为每个相应的电子表格绘制它们。我希望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

2 个答案:

答案 0 :(得分: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?

答案 1 :(得分:1)

您在ActiveSheet上应用所有说明。您可以按Sheets(1).ActivateSheets("sheet_name").Activate选择特定工作表。

您还可以通过

遍历所有工作表
For Each sht In ActiveWorkbook.Sheets
    If sht.Name Like "..." Then ...
Next sht