VBA添加系列重叠数据

Excel 2013,赢10

我试图在一个图表上显示两个数据透视表。 (我不能合并源数据,因为它太多的行)所以我决定添加到VBA的图表系列:

Sub createProductionChart() Sheets("ProductionChart").Select ActiveSheet.ChartObjects("Chart 2").Activate ActiveChart.ChartArea.Select For Each s In ActiveChart.SeriesCollection s.Delete Next s ActiveChart.ChartType = xlAreaStacked 'Add history For i = 2 To 41 If IsEmpty(Worksheets("History Pivot").Cells(6, i)) Then Exit For End If Set tSeries = ActiveChart.SeriesCollection.NewSeries tSeries.XValues = Worksheets("History Pivot").Range("$A$7:$A$300") tSeries.Values = Worksheets("History Pivot").Range(Cells(7, i).Address, Cells(300, i).Address) tSeries.Name = Worksheets("History Pivot").Cells(6, i).Value Next i ' add forecast: For j = 2 To 200 If IsEmpty(Worksheets("Forecast Pivot").Cells(7, j)) Then Exit For End If Set tSeries = ActiveChart.SeriesCollection.NewSeries tSeries.XValues = Worksheets("Forecast Pivot").Range("$A$8:$A$300") tSeries.Values = Worksheets("Forecast Pivot").Range(Cells(8, j).Address, Cells(300, j).Address) If IsEmpty(Worksheets("Forecast Pivot").Cells(6, j)) Then yr = Worksheets("Forecast Pivot").Cells(6, j - 1).Value Else yr = Worksheets("Forecast Pivot").Cells(6, j).Value End If tSeries.Name = yr & " " & Worksheets("Forecast Pivot").Cells(7, j).Value Next j End Sub 

源数据序列在X中是不相交的(它们是date,两个图表分别表示历史和预测数据)。
XL只是将它们堆叠在一起: 在这里输入图像说明

“预测”数据应该在历史的右侧。

根据要求,源数据的样本 – 它们很大,所以只显示左上angular:历史logging: 在这里输入图像说明

预测: 在这里输入图像说明 有任何想法吗?

预测位于历史logging之上的原因是因为堆积区域图始终根据数据的顺序(即使您的区域的值在将来)alignment它的数据。

你需要将历史中的所有date也添加到预测的XValues中(就像它们的值为0)。

我不确定我是否清楚,请告诉我是否需要更多的澄清。

无论如何,请尝试下面的代码,它与我的工作簿和图表上的testing一起工作:

 Sub createProductionChart() Dim Rng_Hisotry As Range Dim Rng_Forecast As Range Dim Rng_Combined As Range Sheets("ProductionChart").Select ActiveSheet.ChartObjects("Chart 2").Activate ActiveChart.ChartArea.Select For Each s In ActiveChart.SeriesCollection s.Delete Next s ActiveChart.ChartType = xlAreaStacked Set Rng_Hisotry = Worksheets("Forecast Pivot").Range("$A$8:$A$300") Set Rng_Forecast = Worksheets("Forecast Pivot").Range("$A$8:$A$300") ' add History with Forecast Range >> so the forecast data will start ' from the correct position on the chart Set Rng_Combined = Range(Rng_Hisotry.Address & ":" & Rng_Forecast.Address) ' Add History For i = 2 To 41 If IsEmpty(Worksheets("History Pivot").Cells(6, i)) Then Exit For End If Set tSeries = ActiveChart.SeriesCollection.NewSeries tSeries.XValues = Rng_Hisotry tSeries.Values = Worksheets("History Pivot").Range(Cells(7, i).Address, Cells(300, i).Address) tSeries.Name = Worksheets("History Pivot").Cells(6, i).Value Next i ' Add Forecast For j = 2 To 200 If IsEmpty(Worksheets("Forecast Pivot").Cells(7, j)) Then Exit For End If Set tSeries = ActiveChart.SeriesCollection.NewSeries tSeries.XValues = Rng_Combined tSeries.Values = Worksheets("Forecast Pivot").Range(Cells(8, j).Address, Cells(300, j).Address) If IsEmpty(Worksheets("Forecast Pivot").Cells(6, j)) Then yr = Worksheets("Forecast Pivot").Cells(6, j - 1).Value Else yr = Worksheets("Forecast Pivot").Cells(6, j).Value End If tSeries.Name = yr & " " & Worksheets("Forecast Pivot").Cells(7, j).Value Next j End Sub