Excel 2013,Win 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中是不相交的(它们是日期,两个图表代表历史和预测数据)。
XL只是将它们堆叠在一起:
"预测"数据应位于历史记录的右侧。
答案 0 :(得分:0)
预测位于历史记录之上的原因是因为 堆积区域图 始终根据数据的顺序对齐数据(即使您的范围的值是将来的。)
您需要将历史记录中的所有日期也添加到预测XValues中(并且它们的值为0)。
我不确定我是否清楚,如果您需要更多说明,请告诉我。
无论如何,尝试下面的代码,它与我在工作簿和图表上进行的测试一起工作:
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