我无法在 VBA Excel 中创建包含超过 5 个数据系列的组合图。最多 5 个数据系列,它工作正常,当我有第 6 个数据系列时,我收到错误:“运行时错误 1004,参数无效”。正如您所看到的,数据并不多,ComboChart 中超过 5 个数据系列似乎存在问题。第一个系列是条形图(主要 y 轴),其余系列是散点图(全部位于次要 y 轴)。请告知这里可能发生了什么。
这是我的代码
Sub CreateComboChart()
Dim ws As Worksheet
Dim chtObj As ChartObject
Dim cht As Chart
Dim rngData As Range
Dim rngCategories As Range
' Set the worksheet
Set ws = ThisWorkbook.Worksheets("Perf")
Set rngData = ws.Range("B2:F10")
Set rngCategories = ws.Range("B1:F1")
' Create a new chart
Set chtObj = ws.ChartObjects.Add(Left:=100, Width:=500, Top:=100, Height:=300)
Set cht = chtObj.Chart
' Set the chart type to combo
cht.ChartType = xlColumnClustered
' Add data to the chart
cht.SetSourceData Source:=rngData
cht.SeriesCollection(1).XValues = rngCategories
cht.FullSeriesCollection(1).AxisGroup = 1
' Set the chart type for the first row (bars)
cht.SeriesCollection(1).ChartType = xlColumnClustered
cht.FullSeriesCollection(2).AxisGroup = 2
cht.SeriesCollection(2).ChartType = xlXYScatter
cht.FullSeriesCollection(2).MarkerStyle = 8
cht.FullSeriesCollection(3).AxisGroup = 2
cht.SeriesCollection(3).ChartType = xlXYScatter
cht.FullSeriesCollection(3).MarkerStyle = 3
cht.FullSeriesCollection(4).AxisGroup = 2
cht.SeriesCollection(4).ChartType = xlXYScatter
cht.FullSeriesCollection(4).MarkerStyle = 8
cht.FullSeriesCollection(5).AxisGroup = 2
cht.SeriesCollection(5).ChartType = xlXYScatter
cht.FullSeriesCollection(5).MarkerStyle = 3
' STOPS here
cht.FullSeriesCollection(6).AxisGroup = 2
cht.SeriesCollection(6).ChartType = xlXYScatter
cht.FullSeriesCollection(6).MarkerStyle = 8
cht.FullSeriesCollection(7).AxisGroup = 2
cht.SeriesCollection(7).ChartType = xlXYScatter
cht.FullSeriesCollection(7).MarkerStyle = 3
End Sub
这是数据
Plant P1 P2 P3 P4 P5
A1 1500 7600 1300 1800 1500
A2 11.5% 12.2% 3.6% 3.3% 9.9%
A3 17.8% 19.8% 0.0% 0.0% 13.4%
A4 10.1% 10.1% 3.5% 1.4% 9.1%
A5 14.7% 15.0% 13.0% 15.1% 11.3%
A6 47.6% 55.3% 24.1% 27.2% 36.5%
A7 13.5% 11.9% 12.2% 14.0% 10.6%
A8 43.1% 40.7% 21.8% 23.6% 33.3%
在上面的代码中,如果我更改为 Set rngData = ws.Range("B2:F6"),并删除系列集合 6 和 7,我会得到此图表。但不适用于“B2:F9”。
您的问题可能是“按行绘制”与“按列绘制”...
我更喜欢逐一添加每个系列,因为它可以让您更好地控制:
Sub CreateComboChart()
Dim ws As Worksheet, chtObj As chartObject
Dim cht As Chart, rngData As Range
Dim rngCategories As Range, i As Long, ms
' Set the worksheet
Set ws = ThisWorkbook.Worksheets("Perf")
ws.ChartObjects(1).Delete 'remove existing plot
Set rngData = ws.Range("B2:F10")
Set rngCategories = ws.Range("B1:F1")
' Create a new chart
Set chtObj = ws.ChartObjects.Add(Left:=100, Width:=500, Top:=100, Height:=300)
Set cht = chtObj.Chart
'remove any "auto-plotted" series from the chart
Do While cht.SeriesCollection.Count > 0
cht.SeriesCollection(1).Delete
Loop
'add the first series (bars)
With cht.SeriesCollection.NewSeries
.ChartType = xlColumnStacked
.XValues = rngCategories
.Values = rngData.Rows(1)
.AxisGroup = 1
End With
ms = 8 'marker style
'add the rest of the series
For i = 2 To rngData.Rows.Count
With cht.SeriesCollection.NewSeries
.ChartType = xlXYScatter
.XValues = rngCategories
.Values = rngData.Rows(i)
.AxisGroup = 2
.MarkerStyle = ms
ms = IIf(ms = 8, 3, 8) 'toggle marker style
End With
Next i
End Sub
剧情: