2016-12-28 629 views
0

我製作了一張圖表,我嘗試繪製一些特定數據。爲此我做了這段代碼:VBA:使用VBA編輯excel圖表中的圖例名稱

Sub plotsim() 
Dim sh As Worksheet 
Dim chrt As Chart 
worksh = Application.Sheets.Count 
Set sh = ActiveWorkbook.Worksheets("Simulation") 
Set chrt = sh.Shapes.AddChart.Chart 
With chrt 
    .ChartType = xlLine 
    .SeriesCollection.NewSeries 
    .SeriesCollection(1).Name = "=""Portfolio forecast""" 
    .SeriesCollection(1).XValues = Sheets("Simulation").Range("A2:A" & fsize + 1) 
    .SeriesCollection(1).Values = Sheets("Simulation").Range(Sheets("Simulation").Cells(2, worksh + 1), Sheets("Simulation").Cells(fsize + 1, worksh + 1)) 
End With 
End Sub 

但是,一旦我看看圖表,我看到的是一個有兩條不同線條的圖例。

如何刪除(或編輯)Series2標題?

+0

據我所知,你可以單擊該圖表,然後再次單擊圖例,然後選擇「系列2」而已,然後就按' DELETE'鍵。 – BruceWayne

回答

1

如果你真的想在圖例編輯系列2你會改變它,你改變了系列一名稱相同的方式:

.SeriesCollection(2).Name = "Unwanted series" 

enter image description here


注意:我最初回答如下:

下面的代碼行添加您不需要的系列2

.SeriesCollection.NewSeries 

簡單地將其刪除。

但我現在看到它並不完全正確。有時會創建一個額外的,有時不會,這取決於表單上過去發生的事情(我認爲我錯過了一些明顯的東西)。我已經能夠重現這兩種行爲。只要你使用了正確的序列號

.SeriesCollection(2).Delete 

:假如你不小心與另外系列的結束,你可以刪除任何多餘的人一起通話。你可以包括這樣一個測試,看看是否額外的一個已經取得:

If .SeriesCollection.Count > 1 then .SeriesCollection(2).Delete