2010-05-24 65 views
0

我有一些Visual Basic代碼爲每一行創建一個圖表。動態圖表系列標籤

.SeriesCollection(1).Values = 「=」 & Ws.Name & & CurrRow & 「C3:R」 「R」!:& CurrRow & 「C8」

它使用這個代碼設置一系列值

我在苦苦掙扎的是如何設置系列標籤?系列標籤將始終是第一行,並位於相應的列中。我知道這比上面的代碼簡單得多,但我很難過。

任何幫助表示讚賞。

回答

1

在.SeriesCollection(1).ApplyDataLabels方法中使用Ws.Range(「C1:H1」)。值。

+0

這些都是數據標籤,而不是系列名稱。 – 2015-08-17 14:54:32

+0

是的,這是OP實際要求的。 – 2015-08-18 15:07:10

+0

直到Excel 2013才能使用數據標籤範圍;在他發佈之後,OP不可能在2010年以後使用任何版本。即使在Excel 2013中,您也無法在ApplyDataLabels方法中分配範圍,您必須在另一步中添加該範圍。在早期版本中,您需要獨立填充每個標籤的文本。 – 2015-08-22 15:25:01

0

我從你的Y值命令猜測,你想這樣:

.SeriesCollection(1).Name = "=" & Ws.Name & "!R" & CurrRow & "C2" 

這是同一行的Y值,列前的Y值開始。

0

我的第一個答案將問題解釋爲詢問系列名稱。如果您想爲數據標籤使用範圍,那有點棘手。

如果您有Excel 2013,您可以直接指定一個範圍:

With .SeriesCollection(1) 
    .ApplyDataLabels 
    With .DataLabels 
    .Format.TextFrame2.TextRange.InsertChartField _ 
     msoChartFieldRange, "='" & Ws.Name & "'!R1C3:R1C8", 0 
    .ShowRange = True 
    .ShowValue = False 
    End With 
End With 

如果您有Excel的早期版本,你要踩通過標籤。

這就賦予細胞對標籤的靜態文本:

Dim iPt As Long 
Dim rLabels As Range 

Set rLabels = Ws.Range(Ws.Cells(1, 3), Ws.Cells(1, 8)) 

With .SeriesCollection(1) 
    .ApplyDataLabels 
    For iPt = 1 To .Points.Count 
    .Points(iPt).DataLabel.Text = rLabels.Cells(iPt).Text 
    Next 
End With 

該鏈接的每個標籤貼到相應的單元格,所以標籤反映在細胞中的任何改變:

Dim iPt As Long 
Dim rLabels As Range 

Set rLabels = Ws.Range(Ws.Cells(1, 3), Ws.Cells(1, 8)) 

With .SeriesCollection(1) 
    .ApplyDataLabels 
    For iPt = 1 To .Points.Count 
    .Points(iPt).DataLabel.Text = "=" & rLabels.Cells(iPt).Address(, , , True) 
    Next 
End With