Through searching online I have been able to utilize the following code to delete all labels in my graphs that correspond to values of 0. Now unfortunately I need a code that doesn’t delete based on the actual values in my data, but rather the text within the labels. Because I use another data table (i.e. “Value From Cells” setting instead of “Value” in the Label options) for the actual text in the labels that correspond to the graphs in my chart.
I tried to include a part where I use IF AND, but I don’t know how to properly implement so that the deletion happens based on the two conditions.
Sub Zero_Label_Remover()
Dim cht As Chart
Dim chtObj As ChartObject
For Each chtObj In ActiveSheet.ChartObjects
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)
With ser.Points(i)
If .HasDataLabel AND DataLabel.Text = "" Then
.DataLabel.Delete
End If
End With
End If
Next i
Next ser
Next chtObj
End Sub
So I tried to change the parameter it looks for so that it deletes all labels that are empty, but this code doesnt work at all. (It works if I remove the if clause in the sense that it just deletes all labels)
Sub Zero_Label_Remover()
'runs through every chart on the ActiveSheet
Dim cht As Chart
Dim chtObj As ChartObject
For Each chtObj In ActiveSheet.ChartObjects
Set cht = chtObj.Chart
Dim ser As Series
Dim pt As Point
For Each ser In cht.SeriesCollection
Dim vals As Variant
vals = ser.Values
If pt.DataLabel.Text = "" Then
pt.HasDataLabel = False
For Each pt In ser.Points
pt.HasDataLabel = False
Next
Next ser
Next chtObj
End Sub
Any input that someone could give me would be hihgly appreciated!