vba - Excel源数据方向错误

标签 vba excel graph

我在 .SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle 行得到参数无效

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

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

为了帮忙,这里有一张 table

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 个系列集合。对于这张 table ,seriesCollection(1) is D4:G4
我想要的垂直系列,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

最佳答案

感谢@Byron Wall,使用 .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

关于vba - Excel源数据方向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31440643/

相关文章:

excel - 确定数组中最大字符串长度的更快方法是什么?

VBA复杂的Getter、Setter语法

VBA Excel 找不到 DLL

vba - 如何将焦点集中到消息框?

python - 为具有代表 'group' 的多条线的图表创建图例

algorithm - 寻找最可靠的路径——Dijkstra算法

vba - 将 Iferror 函数添加到 VBA

c# - 如何使用 NetOffice 重命名 Excel 工作表?

java - 根据自定义值对 HashMap 进行排序(例如,它是一个名为 DATA 的类)

python - 用 C++ 绘制图形