Name

datalabel AutoText [= setting]

Synopsis

True automatically generates the caption of the data label based on its context; False uses the Caption setting instead. Default is True.

Setting the Caption property automatically sets this property to False. For example, the following code turns off Auto Text by setting the data labels for a series:

Sub SetDataLabels( )
    Dim chrt As Chart, sr As Series, dl As DataLabel
    Set chrt = ActiveChart
    chrt.ChartType = xlColumnClustered
    ' Get the first series.
    Set sr = chrt.SeriesCollection(1)
    ' Create data labels.
    sr.HasDataLabels = True
    ' Set data label captions.
    For Each dl In sr.DataLabels
        dl.Caption = sr.Name & ": " & dl.Caption
    Next
End Sub

To restore Auto Text, set the AutoText property to True:

Sub RestoreAutoDataLabels( )
    Dim chrt As Chart, sr As Series
    Set chrt = ActiveChart
    chrt.ChartType = xlColumnClustered
    ' Get the first series.
    Set sr = chrt.SeriesCollection(1)
    ' Make sure data labels exist.
    sr.HasDataLabels = True
    ' Restore Auto Text.
    sr.DataLabels.AutoText = True
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.