循环代码继续使用不同的系列制作图表

时间:2014-01-17 04:26:42

标签: vba charts

这是我第一次学习和使用VBA,请耐心等待。

我已经制定了这个临时代码来绘制xvalues与y值,并且到目前为止一切都很棒(我认为)。现在,我想创建一些东西,使用只需要向下移动4行的数据制作第二,第三,第四(等等)图形。有人可以帮忙吗?!

Sub VOCChartMaker()

    Sheets("HousevGarageCharts").Select

    With ActiveSheet
        .Shapes.AddChart.Select

    With ActiveChart
        .ChartType = xlXYScatter
        .HasTitle = True
        .ChartTitle.Text = "='Data'!$C$2"

        Do Until ActiveChart.SeriesCollection.Count = 0
        ActiveChart.SeriesCollection(1).Delete
        Loop

        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = "='Data'!$A$2"
        .SeriesCollection(1).XValues = "='Data'!$D$2:$BB$2"
        .SeriesCollection(1).Values = "='Data'!$D$3:$BB$3"
        .SeriesCollection(1).MarkerSize = 5
        .SeriesCollection.NewSeries
        .SeriesCollection(2).Name = "='Data'!$A$4"
        .SeriesCollection(2).XValues = "='Data'!$D$4:$BB$4"
        .SeriesCollection(2).Values = "='Data'!$D$5:$BB$5"
        .SeriesCollection(2).MarkerSize = 5

        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Garage"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "House"


    End With

    End With


End Sub

非常感谢!

梅根

1 个答案:

答案 0 :(得分:0)

我没有提供完整的解决方案,而是在部分代码中添加了一些关键点+注释。我认为在那之后其余部分会很容易。

Sub VOCChartMaker()

    Sheets("HousevGarageCharts").Select

'>> we need a simple loop which will star here
    Dim i As Byte  '>>required declaration
    For i=1 To 4   '>>for four charts

    With ActiveSheet
        .Shapes.AddChart.Select

    With ActiveChart
        .ChartType = xlXYScatter
        .HasTitle = True

        '>> title for every fourth row starting from 2nd
        .ChartTitle.Text = "='Data'!$C$" & 2 + (i-1)*4

        '>> as far as you create new chart you don't need it _
            as there is nothing to delete

        'Do Until ActiveChart.SeriesCollection.Count = 0
        'ActiveChart.SeriesCollection(1).Delete
        'Loop

        'changes made to create chart series for every fourth row starting from 2nd
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = "='Data'!$A$" & 2 + (i-1)*4
        .SeriesCollection(1).XValues = "='Data'!$D$" & 2 +  (i-1)*4 & ":$BB$" & 2 +  (i-1)*4
        .SeriesCollection(1).Values = "='Data'!$D$" & 3 +  (i-1)*4 & ":$BB$" & 3 +  (i-1)*4
        .SeriesCollection(1).MarkerSize = 5

        '>> change the second series accordingly on your own!!!!!
            .SeriesCollection.NewSeries
            .SeriesCollection(2).Name = "='Data'!$A$4"
            .SeriesCollection(2).XValues = "='Data'!$D$4:$BB$4"
            .SeriesCollection(2).Values = "='Data'!$D$5:$BB$5"
            .SeriesCollection(2).MarkerSize = 5

        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Garage"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "House"


    End With

    End With

    Next i  '>> the end of the loop

End Sub