2016-08-11 95 views
2

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只是成堆他們在彼此的頂部: enter image description here

的「預測」數據應該在歷史正確的。

按照要求源數據的樣本 - 他們是巨大的,因此,只有左上角所示: 歷史: enter image description here

預測: enter image description here 任何想法?

+0

你可以上傳屏幕截圖?這將有助於更好地理解(或消除導致此行爲的一些原因) –

+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 
+0

我有一種感覺,這是問題所在。我很欣賞這種努力,但這不起作用。這「逆轉」了問題,但看起來很接近 - 它只顯示「預測」數據範圍。 – Marc

+0

@Marc你是什麼意思「逆轉」?我有整個圖表,歷史和預測,不是嗎?你是否改變了圖表的X軸?或者它在自動? –

+0

@Marc沒有評論?我認爲你能做的最低要求是回答那些幫助我解決問題的問題 –