Excel 2013,贏10VBA添加系列重疊數據
我想要在一個圖表上顯示兩個數據透視表。 (因爲它的行太多,我不能合併的源數據。)所以我決定將系列添加到圖表中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只是成堆他們在彼此的頂部:
的「預測」數據應該在歷史正確的。
按照要求源數據的樣本 - 他們是巨大的,因此,只有左上角所示: 歷史:
你可以上傳屏幕截圖?這將有助於更好地理解(或消除導致此行爲的一些原因) –
檢查我的答案和代碼 –