Add data labels from range (text) to multiple (but not all) charts in a sheet

MISCLY

New Member
Joined
Sep 18, 2017
Messages
2
Hi,

As the title states, I'm trying to add the same range of data labels (initials) to 9 out of a total of 13 charts on one sheet. I'm using this popular macro to add labels successfully to 1 chart (Chart 1):

Code:
Sub AddDataLabels()'   Disable screen updating while the subroutine is run.
    Application.ScreenUpdating = False
    
    Dim seSales As Series
    Dim pts As Points
    Dim pt As Point
    Dim rngLabels As Range
    Dim iPointIndex As Integer
    
    Set rngLabels = Range("AutoCalculations!$B$2:$B$50")
     
    Set seSales = Sheets("AutoReport").ChartObjects("Chart 1").Chart.SeriesCollection(1)
    seSales.HasDataLabels = True
     
    Set pts = seSales.Points
    For Each pt In pts
        iPointIndex = iPointIndex + 1
        pt.DataLabel.Text = rngLabels.Cells(iPointIndex).Text
        pt.DataLabel.Font.Bold = False
        pt.DataLabel.Position = xlLabelPositionCenter
    Next pt

End Sub


I would sincerely appreciate help in finding a - smooth - solution to this, that allows me to select which charts should have the labels added to them. I've tried fooling around with ChartObject.Array() and some other things but without success. If you need additional information I'll be glad to assist.

Best,
Richard
 

FormR

MrExcel MVP
Joined
Aug 18, 2011
Messages
6,216
Office Version
365
Platform
Windows
Hi, welcome to the board!

Maybe it's easier to list the charts you want to exclude:

Code:
Sub AddDataLabels() '   Disable screen updating while the subroutine is run.
    Application.ScreenUpdating = False
    
    Dim seSales As Series
    Dim pts As Points
    Dim pt As Point
    Dim rngLabels As Range
    Dim iPointIndex As Integer
    Dim Ch As ChartObject
    
    Set rngLabels = Range("AutoCalculations!$B$2:$B$50")
         
    For Each Ch In Sheets("AutoReport").ChartObjects
        If Ch.Name <> "Chart 2" And Ch.Name <> "Chart 3" And Ch.Name <> "Chart 4" Then 'List of Charts to exlude
            Set seSales = Ch.Chart.SeriesCollection(1)
            seSales.HasDataLabels = True
         
            Set pts = seSales.Points
            iPointIndex = 0
            For Each pt In pts
                iPointIndex = iPointIndex + 1
                pt.DataLabel.Text = rngLabels.Cells(iPointIndex).Text
                pt.DataLabel.Font.Bold = False
                pt.DataLabel.Position = xlLabelPositionCenter
            Next pt
        End If
    Next Ch


End Sub
 

MISCLY

New Member
Joined
Sep 18, 2017
Messages
2
Hi, welcome to the board!

Maybe it's easier to list the charts you want to exclude:

Code:
Sub AddDataLabels() '   Disable screen updating while the subroutine is run.
    Application.ScreenUpdating = False
    
    Dim seSales As Series
    Dim pts As Points
    Dim pt As Point
    Dim rngLabels As Range
    Dim iPointIndex As Integer
    Dim Ch As ChartObject
    
    Set rngLabels = Range("AutoCalculations!$B$2:$B$50")
         
    For Each Ch In Sheets("AutoReport").ChartObjects
        If Ch.Name <> "Chart 2" And Ch.Name <> "Chart 3" And Ch.Name <> "Chart 4" Then 'List of Charts to exlude
            Set seSales = Ch.Chart.SeriesCollection(1)
            seSales.HasDataLabels = True
         
            Set pts = seSales.Points
            iPointIndex = 0
            For Each pt In pts
                iPointIndex = iPointIndex + 1
                pt.DataLabel.Text = rngLabels.Cells(iPointIndex).Text
                pt.DataLabel.Font.Bold = False
                pt.DataLabel.Position = xlLabelPositionCenter
            Next pt
        End If
    Next Ch


End Sub
Thank you for the welcome FormR. It definitely seems like a sound idea to exclude the charts instead, and your code works perfectly - thank you for the assistance and have a great week!
 

Forum statistics

Threads
1,081,996
Messages
5,362,612
Members
400,684
Latest member
Vie

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top