Company News Products Projects Texts Museum Download Contact Map RSS Polski
YAC Software Texts VBA Removing small value labels from PowerPoint charts Trybiks' Dive
  Back

List

Data

Excel

Media research

Market research

Respondent quotas

SPSS

VBA

YAC Data Language

Removing small value labels from PowerPoint charts
Value labels on charts help in understanding the presented data. However, if there are too many labels, the less important ones tend to clutter the image. Removing them may help in making the results easier to analyze.

Consider the following image:



Those values lower than 4-5% don't really add anything to the slide; they just clutter up the image, making it harder to decipher.

Now, remove labels of values lower than 5%, and you get this:



Not only easier to understand, but looks better too.

Now, the macro to do that isn't very complicated. The usual approach would be to record a simple macro (that deletes one value, for instance), and work out how to loop through the different labels on a slide (and perhaps all slides in a presentation). But since Microsoft decided to drop macro recording in PowerPoint (WTF?), it's not that easy to come up with the code. So, here's the code, and the explanations follow.
  Sub RemoveSmallPercentsFromChart(chart As Chart)
  
    Const valueThreshold As Double = 0.05
  
    For Each serie In chart.SeriesCollection
      If serie.HasDataLabels Then
        Dim pointCount As Integer
        Dim pointValues As Variant
  
        pointCount = serie.Points.Count
        pointValues = serie.Values
  
        For pointIndex = 1 To pointCount
          If pointValues(pointIndex) < valueThreshold Then
            serie.Points(pointIndex).HasDataLabel = False
          End If
        Next pointIndex
      End If
    Next
  
  End Sub
This first method handles the actual hiding of data labels, given a chart. valueThreshold is the limit below which data labels will be hidden. The first loop loops through all series of a chart; the second loop - through all points of a serie. If it finds a point that has a value below the threshold - it hides its data label (by setting HasDataLabel to False).


  Sub RemoveSmallPercentsFromAllCharts()
  
    For Each slide In ActivePresentation.Slides
      For Each shape In slide.Shapes
        If shape.HasChart Then
          Set chart = shape.Chart
          RemoveSmallPercentsFromChart chart
        End If
      Next
    Next
  
  End Sub
This method loops through all slides and all shapes on these slides; if it finds a shape that contains a chart, it calls the first method to clean up the chart.


  Sub RemoveSmallPercentsFromSelectedChart()
  
    Set selection = Windows(1).Selection
  
    If selection.Type = ppSelectionShapes Then
      If selection.ShapeRange.Type = msoChart Or _
         selection.ShapeRange.Type = msoPlaceholder Then
        Set chart = selection.ShapeRange.Item(1).Chart
        RemoveSmallPercentsFromChart chart
      Else
        MsgBox "Please select a chart first."
      End If
    Else
      MsgBox "Please select a chart first."
    End If
  
  End Sub
Finally, a method to clean up the chart select by the user (a chart will be selected when any part of the chart is clicked). If a shape containing a chart is currently selected, the first method is called to clean up that chart.

HTH

Top

Comments
#1
Vascool wrote on 2012-04-19 12:33:47
I have copied all 3 codes in PPT 2010 and i found Compile Error: ByRef argument type mismatch in below code

RemoveSmallPercentsFromChart chart

Please help
#2
trybik wrote on 2012-04-19 17:29:48
Hmm... I don\'t have PPT 2010 with me right now, but just try removing the \'As Chart\' part from the declaration of the first sub.

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

VBA


Related pages

Open All and Merge