循环中创建的第一个图表与后续图表不同

时间:2018-04-11 18:33:44

标签: excel vba loops charts

我有一个循环,可以从一组数据中自动生成图表。我的问题是第一张图表总是与后续图表不同。任何人都可以帮助解释为什么我的第一张图表总是与后续图表不同?

以下是运行代码后图表的样子:

Charts

这是我的代码:

Sub Pyramid()

 Sheets("Pyramid").Select
 Range("C2").Select
 ActiveCell.Range("B1:J1").Select


    For counter = 2 To 5
        ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
        ActiveChart.ApplyChartTemplate ( _
            "C:\Users\arboari\AppData\Roaming\Microsoft\Templates\Charts\1Pyramid.crtx")
        With ActiveChart
             Do While .SeriesCollection.Count > 0
                .SeriesCollection(1).Delete
             Loop
            .SeriesCollection.NewSeries.Values = Range("'Pyramid'!$W$2" & ":$AN$2") 'BC males
            .SeriesCollection.NewSeries.Values = Range("'Pyramid'!$W$3" & ":$AN$3") 'BC females
            .SeriesCollection.NewSeries.Values = Range("'Pyramid'!$C$" & counter & ":$K$" & counter) 'Community males
            .SeriesCollection.NewSeries.Values = Range("'Pyramid'!$M$" & counter & ":$U$" & counter) 'Community females
                .FullSeriesCollection(1).Name = "='Pyramid'!$V$2" 'BC males
                .FullSeriesCollection(2).Name = "='Pyramid'!$V$3" 'BC females
                .FullSeriesCollection(3).Name = "='Pyramid'!$B$2" 'Community males
                .FullSeriesCollection(4).Name = "='Pyramid'!$L$2" 'Community females
        End With

        ActiveChart.Location Where:=xlLocationAsObject, Name:="Charts"

        With ActiveChart
            .HasTitle = False
            .Axes(xlCategory).MinimumScale = -15
            .Axes(xlCategory).MaximumScale = 15
            .FullSeriesCollection(3).Select
                With Selection.Format.Fill
                    .Visible = msoTrue
                    .Patterned msoPatternLightVertical
                End With
                Selection.AxisGroup = 2
            .FullSeriesCollection(4).Select
                With Selection.Format.Fill
                    .Visible = msoTrue
                    .Patterned msoPatternLightVertical
                End With
                Selection.AxisGroup = 2
            .Parent.Top = 50
            .Parent.Left = c * 130
        End With
        Sheets("Charts").Select

        c = c + 3
    Next counter
Range("J1").Select
End Sub

谢谢!

0 个答案:

没有答案