2

I have a code that removes the data label(s) from a customized pie chart if the value of the cell is 0%. However, since my code loops so that the data changes, i completely lose the label for that particular category so when a new set of data is added and the value is not zero the label does not appear any more. How do I do it so that when the value is 0 the data label is removed but when the value is anything but zero then it would reappear, essentially resetting the original set up of the chart so that all values/categories have data labels.

  Sub ChartLoop()

       Range("D2").Select
        ActiveCell.Range("C1:E1").Select

    Dim myPDF As String
    Dim i As Long

        For counter = 2 To 21

            Sheets("CF").Select
            Range("'CF'!$D$" & counter & ":$F$" & counter).Select 'numbers
            Selection.Copy
            Sheets("CF-Chart").Select
            Range("B1:B3").Select
            Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
                False, Transpose:=True

  'this is for removing the data labels        
        Dim iPts As Integer
        Dim nPts As Integer
        Dim aVals As Variant
        Dim srs As Series

        ActiveSheet.ChartObjects("Chart 5").Activate
            For Each srs In ActiveChart.SeriesCollection
                With srs
                    If .HasDataLabels Then
                        nPts = .Points.Count
                        aVals = .Values
                        For iPts = 1 To nPts
                            If aVals(iPts) = 0 Then
                                .Points(iPts).HasDataLabel = False
                            End If
                        Next
                    End If
                End With
            Next


           ActiveSheet.ChartObjects("Chart 5").Activate
           ActiveChart.ChartArea.Select
           myPDF = "\\stchsfs\arboari$\Profile-Data\Desktop\Export Trial1\c2-" & Sheets("CF").Range("C" & i + 2).Value2 & ".pdf"
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myPDF, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        i = i + 1
        Next counter

    End Sub

The first chart is how my regular chart would look like. The second chart is where I would want to remove the data label with the value 0 but keep the data label with categories and value for the other ones with value >0. Chart1 Chart2

Thanks!

1 Answer 1

1

Maybe change

If aVals(iPts) = 0 Then
    .Points(iPts).HasDataLabel = False
End If

to this?

If aVals(iPts) = 0 Then
    .Points(iPts).HasDataLabel = False
    .DataLabels.ShowValue = False
Else
    .Points(iPts).HasDataLabel = True
    .DataLabels.ShowValue = True
End If

EDIT 4-27-2018

Okay... I've tested this solution and it works for me. Not the most elegant but it works. Let me know if it works for you -

ActiveSheet.ChartObjects("Chart 5").Activate

With ActiveChart.SeriesCollection(1)
    For i = 1 To .Points.Count
        If .Points(i).HasDataLabel = False Then
            .Points(i).Select
            ActiveChart.SetElement (msoElementDataLabelShow)
                If .Points(i).DataLabel.Text = 0 Then
                    .Points(i).HasDataLabel = False
                    .Points(i).DataLabel.ShowValue = False
                End If
        ElseIf .Points(i).DataLabel.Text = 0 Then
            .Points(i).HasDataLabel = False
            .Points(i).DataLabel.ShowValue = False
        End If
    Next i
End With
9
  • It works but it removes the category name completely from my labels though. How do I do it so that the category names are included in the labels?
    – amea6995
    Apr 26, 2018 at 17:35
  • @amea6995 I've edited the above code, see if that helps.
    – dwirony
    Apr 26, 2018 at 17:41
  • @amea6995 Actually now I'm realizing that your If statement tests if it has a data label already. Might need to do some more restructuring...
    – dwirony
    Apr 26, 2018 at 17:42
  • Category name still does not show up unfortunately and the new one brings back the data labels with 0 values.
    – amea6995
    Apr 26, 2018 at 17:45
  • @amea6995 Could you possibly add a picture of what your chart looks like, and what you want it to look like after? Sorry, this is a bit difficult one to solve without seeing what exactly you're trying to accomplish.
    – dwirony
    Apr 26, 2018 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.