cellMatrix.net

Spreadsheet Modeling and Related Topics

VBA for Adding and Deleting Data Labels

I'm currently working on a project where I have multiple embedded line charts in a workbook. Each line chart contains any number of multiple series. Each series contains 12 points representing each month of the year. For each chart I need to show the data points for the current month only.

Although the combination Line - XY Scatter chart works, I want to use VBA to add and delete the data labels in each chart in an effort to minimize the number of chart series that the owner of the workbook needs to maintain. Below are some simple macros for adding and deleting data labels that I've used for reference and / or utilized in my project. Each example works for the active chart only (an active single embedded chart).

To delete a label from a single series:

Sub Delete_Labels_From_A_Single_Series()
    For Each X In ActiveChart.SeriesCollection(1).Points
        X.DataLabel.Delete
    Next X
End Sub

A second method for deleting a label from a single series:

Sub Delete_Labels_From_A_Single_Series()
    
    Dim Cht As Chart
    Dim Srs As Series
    
    Set Cht = ActiveChart
    Set Srs = Cht.SeriesCollection(1)
    
        With Srs
            If .HasDataLabels Then .DataLabels.Delete
        End With

End Sub

To add data labels to multiple series at one time:

Sub Add_Data_Labels_To_All_Series()
    
    Dim Cht As Chart
    
    Set Cht = ActiveChart
    
    For Each Sr In Cht.SeriesCollection
        Sr.ApplyDataLabels
        Sr.DataLabels.ShowValue = True
        Sr.DataLabels.Position = xlLabelPositionAbove
    Next Sr

End Sub

To delete data labels from multiple series at one time:

Sub Delete_Data_Labels_From_All_Series()
    
    Dim Cht As Chart
    
    Set Cht = ActiveChart
    
    For Each Sr In Cht.SeriesCollection
        If Sr.HasDataLabels Then Sr.DataLabels.Delete
    Next Sr

End Sub

Posted on Tuesday, December 22, 2009 | Comments (2) | Permalink
Comments

I have a free add-in that labels the last point in each series of a chart:

Label Last Point – Updated Add-In

Posted by Jon Peltier  on  12/24  at  08:20 AM

Thanks for this informative Topic
Keep it up

Posted by bluesea  on  02/20  at  02:50 PM
Page 1 of 1 pages

Comment Entry Form

User Information

Name: (Required)

E-Mail Address: (Optional)

Location: (Optional)

Web Site Address: (Optional)

Comment Formatting Reference
Add VBA Code:
<pre>some code here</pre>
Linking:
[url]http://www.example.com/[/url]
[url=http://www.example.com/]my site[/url]
Text Formatting
[size=4]Some larger text[/size]
[color=green]Some green text[/color]
[b]Some bold text[/b]
[i]Some italic text[/i]
Enter Comment Below

Personalization Options
Remember my personal information
Notify me of follow-up comments?

Next entry: VBA Function to Screen for Certain Charts

Previous entry: Removing Spaces from File Names

<< Back to main

Statistics

  • Total Entries - 136
  • Current Viewers - 25

Categories

Entries by Day

Jul - 2010
S M T W T F S
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Recent Comments

Syndicate