Search code examples
excelvbacharts

Excel VBA selecting & deleting labels based on label text not values


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
            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!


Solution

  • You forgot to tell us what you mean with "doesnt work at all". I guess you are a victim of VBA's ways to evaluate conditions in an AND-clause: It will always evaluate all parts.

    The statement If .HasDataLabel And .DataLabel.Text = "" Then (you forgot a . in front of DataLabel, btw) will check .HasDataLabel AND .DataLabel.Text = "". However, if .HasDataLabel is False (the point has no data label), the term .DataLabel.Text = "" will throw an exception (Method 'DataLabel' of object 'Point' failed).

    In such case, you need to split the command in two separate statements:

            For i = LBound(vals) To UBound(vals)
                With ser.Points(i)
                    If .HasDataLabel Then
                        If .DataLabel.Text = "" Then
                            .DataLabel.Delete
                        End If
                    End If
                End With
            Next i