The following code allows the user to delete Chart Data Labels with a value of zero from a named chart.
To select a chart, the user has to enter the chart name in the macro.
In this example, the name is “Chart abc”.
How do you build the code, so that the user can simply select a chart by clicking on it?
Sub DeleteZeroValueLabelsfromSelectedChart()
' If ActiveChart Is Nothing Then
' MsgBox "No chart selected. Please select a chart."
' Else
Dim cht As Chart
Dim chtObj As ChartObject
Set chtObj = ActiveSheet.ChartObjects("Chart abc") 'SET
Set cht = chtObj.Chart
Dim ser As Series
For Each ser In cht.SeriesCollection
Dim vals As Variant
vals = ser.Values
Dim i As Integer
For i = LBound(vals) To UBound(vals)
If vals(i) = 0 Then
With ser.Points(i)
If .HasDataLabel Then
.DataLabel.Delete
End If
End With
End If
Next i
Next ser
End Sub
How do you say:
Set chtObj = ActiveChart