Excel源数据错误的方向
我得到的参数无效线.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
我有什么:该系列是Horizontaly,取决于季节的数量。 就像上面这个表一样,我得到了3个系列的collections。 再次对于这个表, seriesCollection(1) is D4:G4
我想要的垂直系列,SourceData是"D4:G" & seasonCount + 3
,这将是D4到G6。 使用SeriesCollection(1) = "D4:D6"
,然后删除对应于列E和列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
编辑**
我之前无法添加图片,但现在可以。 这是结果:
这是另一张工作得很好的表,正如你所看到的,代码中没有太多的变化。 不同之处在于seasonCount
variables和X轴现在是A列而不是B的事实。
工作代码和图表:
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
手动创build系列工作得非常好。 这是工作代码
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