Excel 2016图表文本标签

时间:2016-07-31 15:39:30

标签: excel excel-vba vba

在Excel 2016中,我想在图表上显示标签。

我有一个函数alph,它有相应数字的字母:A到1,B到2,AA到27等。

我已经将数据填充到表格(“数据”)中,而图表在没有数据标签的情况下工作。

    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

1 个答案:

答案 0 :(得分:0)

我使用Sub动态地将Series添加到图表中,然后设置每个Series的参数(不同)。

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