2016-07-31 65 views
0

在Excel 2016中,我想在圖表上顯示標籤。Excel 2016圖表文本標籤

我有一個函數,ALPH,具有字母數字對應:A到1,B 2,AA至27等

我有填充到已經表( 「數據」)和數據圖表沒有DataLabels。

For s = 1 To 18 
     ns = (s - 1) * 2 
     Max = "32" 
     Sheets("Analysis").ChartObjects("Chart 1").Activate 
     Sheets("Analysis").ChartObjects("Chart 1").Chart.SeriesCollection(s).XValues = Sheets("Data").Range(alph(ns + 1) & "26:" & alph(ns + 1) & Max) 
     Sheets("Analysis").ChartObjects("Chart 1").Chart.SeriesCollection(s).Values = Sheets("Data").Range(alph(ns + 2) & "26:" & alph(ns + 2) & Max) 
     Sheets("Analysis").ChartObjects("Chart 1").Chart.SeriesCollection(s).Name = nameA(s) 
     Sheets("Analysis").ChartObjects("Chart 1").Chart.SeriesCollection(s).Points(s).DataLabels.HasDataLabel = True 
     Sheets("Analysis").ChartObjects("Chart 1").Chart.SeriesCollection(s).Points(s).DataLabels.Position = xlLabelPositionBelow 
     Sheets("Analysis").ChartObjects("Chart 1").Chart.SeriesCollection(s).Points(s).DataLabels.Text = nameA(s) 
    Next s 
+0

你可以分享你的其他代碼嗎?它將更容易調試?你有錯誤嗎? –

+0

你看過我的答案嗎? –

回答

0

我正在使用Sub動態地將系列添加到圖表,然後它爲每個系列設置參數(這是不同的)。

Option Explicit 

Sub CreateChart() 

Dim Cht         As ChartObject 
Dim Cht_Series       As Series 
Dim Series_Cntr       As Integer 

Set Cht = Worksheets("Analysis").ChartObjects("Chart 1") 

With Cht.Chart 
    Set Cht_Series = .SeriesCollection(s) 
    With Cht_Series 
     .XValues = Sheets("Data").Range(alph(ns + 1) & "26:" & alph(ns + 1) & Max) 
     .Values = Sheets("Data").Range(alph(ns + 2) & "26:" & alph(ns + 2) & Max) 
     .Name = nameA(s) 

     .ApplyDataLabels 
     With .DataLabels 
      .Position = xlLabelPositionBelow 

      ' decide what you want to display on the Data labels 
      .ShowValue = True 
      .ShowSeriesName = True 
      .ShowCategoryName = True 
     End With 
    End With 
End With 

End Sub