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
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0
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!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top