Excel VBA – Pivot table/chart 5 – Chart Data label control

Turn on/off chart data label using VBA code

Sample file downloadhttps://github.com/dannyyun/pivot-vba.git

Turn on/off chart data label

Sub DataLabel()

    Dim cbValue As Object
    Set cbValue = ActiveSheet.CheckBoxes("Check Box 1")
    
    Dim chtObj As ChartObject
    Dim sr As Series
    
    With cbValue
        If .Value = 1 Then
            For Each chtObj In ActiveSheet.ChartObjects
                For Each sr In chtObj.Chart.SeriesCollection
                    sr.ApplyDataLabels
                        With sr.DataLabels
          '             .ShowSeriesName = True
                        .ShowValue = True
           '            .Position = xlLabelPositionInsideBase
          '             .Orientation = -90
          '             .Font.Size = 8
                        End With
                Next sr
            Next chtObj
            
        Else
            For Each chtObj In ActiveSheet.ChartObjects
                For Each sr In chtObj.Chart.SeriesCollection
                    sr.ApplyDataLabels
                        With sr.DataLabels
          '             .ShowSeriesName = True
                        .ShowValue = False
           '            .Position = xlLabelPositionInsideBase
          '             .Orientation = -90
          '             .Font.Size = 8
                        End With
                Next sr
            Next chtObj
        
        End If
    End With
    
End Sub

Leave a Reply

Discover more from this and that @ work

Subscribe now to keep reading and get access to the full archive.

Continue reading