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

List

Data

Excel

Media research

Market research

Respondent quotas

SPSS

VBA

YAC Data Language

Removing small value labels from Excel charts
In the previous article describing a similar macro for PowerPoint Joy asked for the same, but for Excel; so, here it goes:

First, a helper method that handles the hiding of data labels for a given 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 single series. If it finds a point that has a value below the threshold - it hides its data label (by setting HasDataLabel to False). (This code is exactly the same as the respective method in the PowerPoint macro.)
  Sub RemoveSmallValuesFromChart(chart)
  
    Const valueThreshold As Double = 0.05
  
    For Each series In chart.SeriesCollection
      If series.HasDataLabels Then
        Dim pointCount As Integer
        Dim pointValues As Variant
  
        pointCount = series.Points.Count
        pointValues = series.Values
  
        For pointIndex = 1 To pointCount
          If pointValues(pointIndex) < valueThreshold Then
            series.Points(pointIndex).HasDataLabel = False
          End If
        Next pointIndex
      End If
    Next series
  
  End Sub

The next method loops through all worksheets and all charts on these worksheet; if it finds a chart, it calls the first method to clean up the chart. The first part handles charts embedded in standard data worksheets, the second part - charts that are on separate tabs.
  Sub RemoveSmallValuesFromAllCharts()
  
    ' Loop over embedded charts:
    For Each worksheet In Worksheets
      If Not (worksheet.ChartObjects Is Nothing) Then
        For Each chartObject In worksheet.ChartObjects
          RemoveSmallValuesFromChart chartObject.Chart
        Next chartObject
      End If
    Next worksheet
  
    ' Loop over chart sheets:
    For Each chart In Charts
      RemoveSmallValuesFromChart chart
    Next chart
  
  End Sub

Finally, we have the method to clean up the chart selected by the user (a chart will be selected when any part of the chart is clicked).
  Sub RemoveSmallValuesFromSelectedChart()
  
    Set chart = ActiveChart
  
    If chart Is Nothing Then
      MsgBox "Please select a chart first."
    Else
      RemoveSmallValuesFromChart chart
    End If
  
  End Sub
HTH

Top

Comments
#1
new2VBA wrote on 2015-03-03 20:17:41
Thank you so much for this code - it is exactly what I needed, however I am getting a Run-time '424':Object Required error when I run the Sub RemoveSmallValuesFromChart(chart) macro.

I am new to VBA and was not sure what I was doing wrong.

Any help you can provide would be much appreciated!
#2
trybik wrote on 2015-03-04 13:57:40
The first method is just a helper method - it should not be called directly; use one of the other two methods to hide labels in charts.

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

Excel


Related pages

Removing small value labels from PowerPoint charts

Searching for Values in 2D Tables

Referencing Cells in Excel

Open All and Merge