Excel源数据方向错误

时间:2015-07-15 20:41:30

标签: excel vba excel-vba graph

我在第.SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle

获取参数无效

问题是我的源数据似乎不起作用,或者更确切地说,确实有效,但不像我认为的那样。

我无法添加图片,所以我会尽我所能来描述正在发生的事情和我正在寻找的事情。

要提供帮助,请点击此处

3    season A      col B  col C col D    col E   col F   col G
4    2010 - 2011       9,66   1,25  10,9    10175   20837   31012
5    2011 - 2012       7,34   0,62  8       8110    21884   29994
6    2012 - 2013       7,84   0,18  8       6840    17943   24783

哪个seasonCount = 3

我所拥有的:这个系列是水平的,取决于季节的数量。就像上面的这个表一样,我获得了3个系列的收藏。对于此表,再次seriesCollection(1) is D4:G4

我想要的是Vertical Series,SourceData是"D4:G" & seasonCount + 3,它是D4到G6。使用SeriesCollection(1) = "D4:D6"然后删除与col E和col F以及SeriesCollection(2) = "G4:G6"

对应的集合
With ActiveSheet.ChartObjects.Add _
        (Left:=10, Width:=480, Top:=240, Height:=265)
    With .Chart
        .ChartType = xlLineMarkers

        .SetSourceData Source:=Sheets("Results").Range("D4:G" & seasonCount + 3)

        .SeriesCollection(1).XValues = Sheets("Results").Range("A4:A" & seasonCount + 3)
        .SeriesCollection(1).Name = "Indice de rigueur hivernale"
        .SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
        .SeriesCollection(1).Format.Line.Weight = 4
        .SeriesCollection(1).Border.Weight = 0.75

        .SeriesCollection(2).Delete
        .SeriesCollection(2).Delete

        .SeriesCollection(2).ChartType = xlColumnClustered
        .SeriesCollection(2).AxisGroup = 2
        .SeriesCollection(2).Name = "Consommation de sel totale"

        With .SeriesCollection(2).Format.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.349999994
            .Transparency = 0
        End With
        With .SeriesCollection(2).Format.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.25
            .Transparency = 0
            .Solid
        End With

        .SetElement (msoElementChartTitleAboveChart)
        .SetElement (msoElementLegendBottom)
        .SetElement (msoElementPrimaryValueAxisTitleRotated)
        .SetElement (msoElementSecondaryValueAxisTitleRotated)
        .Axes(xlValue, xlPrimary).AxisTitle.Text = "Indice de rigueur hivernale"
        .Axes(xlValue, xlSecondary).AxisTitle.Text = "Consommation de sel (tonnes)"
        .ChartStyle = 19
        .ChartTitle.Text = "Indice par rapport au sel total"
    End With
End With

编辑**

我之前无法添加图片,但现在我可以。这是结果: enter image description here

这是另一张工作正常的表,正如您所看到的,代码中没有太大的变化。区别在于seasonCount变量以及X轴现在是A列而不是B的事实。

工作代码和图表:

EDIT

With ActiveSheet.ChartObjects.Add _
        (Left:=10, Width:=480, Top:=240, Height:=265)
    With .Chart
        .ChartType = xlLineMarkers

        .SetSourceData Source:=Sheets("Results").Range("E4:H10")


        .SeriesCollection(1).XValues = Sheets("Results").Range("B4:B10")
        .SeriesCollection(1).Name = "Indice de rigueur hivernale"
        .SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
        .SeriesCollection(1).Format.Line.Weight = 4
        .SeriesCollection(1).Border.Weight = 0.75

        .SeriesCollection(2).Delete
        .SeriesCollection(2).Delete

        .SeriesCollection(2).ChartType = xlColumnClustered
        .SeriesCollection(2).AxisGroup = 2
        .SeriesCollection(2).Name = "Consommation de sel totale"

        With .SeriesCollection(2).Format.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.349999994
            .Transparency = 0
        End With
        With .SeriesCollection(2).Format.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.25
            .Transparency = 0
            .Solid
        End With

        .SetElement (msoElementChartTitleAboveChart)
        .SetElement (msoElementLegendBottom)
        .SetElement (msoElementPrimaryValueAxisTitleRotated)
        .SetElement (msoElementSecondaryValueAxisTitleRotated)
        .Axes(xlValue, xlPrimary).AxisTitle.Text = "Indice de rigueur hivernale"
        .Axes(xlValue, xlSecondary).AxisTitle.Text = "Consommation de sel (tonnes)"
        .ChartStyle = 19
        .ChartTitle.Text = "Indice par rapport au sel total"
    End With
End With

1 个答案:

答案 0 :(得分:1)

感谢@Byron Wall's,使用.SeriesCollection.NewSeries而不是.SetSourceData手动创建系列效果非常好。这是工作代码

With ActiveSheet.ChartObjects.Add _
        (Left:=10, Width:=480, Top:=240, Height:=265)
    With .Chart
        .ChartType = xlLineMarkers

        .SeriesCollection.NewSeries
        .SeriesCollection(1).Values = Sheets("Results").Range("D4:D" & seasonCount + 3)
        .SeriesCollection(1).XValues = Sheets("Results").Range("A4:A" & seasonCount + 3)
        .SeriesCollection(1).Name = "Indice de rigueur hivernale"
        .SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
        .SeriesCollection(1).Format.Line.Weight = 4
        .SeriesCollection(1).Border.Weight = 0.75

        .SeriesCollection.NewSeries
        .SeriesCollection(2).Values = Sheets("Results").Range("G4:G" & seasonCount + 3)
        .SeriesCollection(2).ChartType = xlColumnClustered
        .SeriesCollection(2).AxisGroup = 2
        .SeriesCollection(2).Name = "Consommation de sel totale"

        With .SeriesCollection(2).Format.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.349999994
            .Transparency = 0
        End With
        With .SeriesCollection(2).Format.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = -0.25
            .Transparency = 0
            .Solid
        End With

        .SetElement (msoElementChartTitleAboveChart)
        .SetElement (msoElementLegendBottom)
        .SetElement (msoElementPrimaryValueAxisTitleRotated)
        .SetElement (msoElementSecondaryValueAxisTitleRotated)
        .Axes(xlValue, xlPrimary).AxisTitle.Text = "Indice de rigueur hivernale"
        .Axes(xlValue, xlSecondary).AxisTitle.Text = "Consommation de sel (tonnes)"
        .ChartStyle = 19
        .ChartTitle.Text = "Indice par rapport au sel total"
    End With
End With