2012-10-16 48 views
0

我正在嘗試將包含多個工作表中的數據的圖表繪製到同一圖表中。因此,每張表格的數據在我的圖表中都是一個單獨的系列。這裏是兩張表的示例代碼:如何使用多個工作表中的數據創建單個圖表

Name1 = ActiveWorkbook.Worksheets(1).Name 
Name2 = ActiveWorkbook.Worksheets(2).Name 

ActiveSheet.Shapes.AddChart.Select 
ActiveChart.ChartType = xlXYScatterSmooth 
ActiveChart.ChartArea.Select 

ActiveChart.SeriesCollection.NewSeries 

ActiveChart.SetSourceData Source:=Range("'" & Name1 & "'!$C$" & nStart & ":$D$" & nLast & "") 
ActiveChart.SeriesCollection(1).Name = "=" & Name1 & "!A1" 
ActiveChart.SeriesCollection(1).XValues = "='" & Name1 & "'!$C$" & nStart & ":$C$" & nLast & "" 
ActiveChart.SeriesCollection(1).Values = "='" & Name1 & "'!$E$" & nStart & ":$E$" & nLast & "" 

ActiveChart.SeriesCollection.NewSeries 
ActiveChart.SetSourceData Source:=Range("'" & Name2 & "'!$C$" & nStart & ":$D$" & nLast & "") 
ActiveChart.SeriesCollection(2).XValues = "='" & Name2 & "'!$C$" & nStart & ":$C$" & nLast & "" 
ActiveChart.SeriesCollection(2).Values = "='" & Name2 & "'!$E$" & nStart & ":$E$" & nLast & "" 
ActiveChart.SeriesCollection(2).Name = "=" & Name2 & "!A1" 

但即時通訊第二系列集合中的「無效參數」錯誤。請幫我解決這個問題。

謝謝!

回答

0

嘗試在第二個系列集合ActiveChart.SetSourceData Source:=Range("'" & Name2 & "'!$C$" & nStart & ":$D$" & nLast & "")中註釋代碼行,然後查看它是否有效。

由於您明確定義了系列的值,所以您甚至不需要第一個代碼行ActiveChart.SetSourceData

您還可以添加這些代碼行以爲圖形指定一個名稱,因爲它在添加第二個系列時被覆蓋;

ActiveChart.SetElement (msoElementChartTitleAboveChart) 
Selection.Caption = "NameOfChart" 

希望幫助

+0

謝謝!刪除'ActiveChart.SetSourceData'行的作品 – PerlDev5

相關問題