Using a named range in VBA for chart data labels

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a drop down box with a Yes or No which calls the macro below to add data labels to my chart, not just the y and x value, but rather, value from cells. Of course the macro below is just for the Yes side of the drop down and that I have all done.

There is a named range for the values, but not getting the right syntax for adding the named range to the code below. Included is what I tried. But no luck.

Code:
Sub Macro4()
'    Dim rng As Range
'    Set rng = Worksheets("Linest").Range("MyLabels")
    ActiveChart.FullSeriesCollection(1).DataLabels.Select
'    ActiveChart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, rng, 0
    ActiveChart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, "=Linest!$BD$16:$BD$33", 0
    Selection.ShowRange = True
    Selection.ShowValue = False
    Application.Goto [A1], Scroll:=True
End Sub

Can somebody point me in the right direction?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You will need to make sure that your series has data labels before running your code. You can use the ApplyDataLabels method of the Series object in order to apply the labels. Maybe something like this...

Code:
Option Explicit

Sub AddDataLabelsFromCells()


    Dim targetChart As Chart
    Dim labelRange As Range
    
    Set targetChart = ActiveChart
    
    If TypeName(targetChart) <> "Chart" Then
        MsgBox "Select a chart, and try again!", vbExclamation
        Exit Sub
    End If
    
    Set labelRange = Worksheets("Linest").Range("MyLabels")
    
    With targetChart.SeriesCollection(1)
        .ApplyDataLabels
        With .DataLabels
            .Format.TextFrame2.TextRange.InsertChartField msoChartFieldRange, labelRange.Address(External:=True), 0
            .ShowCategoryName = False
            .ShowRange = True
            .ShowSeriesName = False
            .ShowValue = False
        End With
    End With
    
End Sub

However, to avoid having to select your chart before running your macro, you can specify your chart by name instead of referring to the active chart. To do so, simply replace...

Code:
    Set targetChart = ActiveChart
    
    If TypeName(targetChart) <> "Chart" Then
        MsgBox "Select a chart, and try again!", vbExclamation
        Exit Sub
    End If

with

Code:
    Set targetChart = Worksheets("Linest").ChartObjects("Chart 1").Chart

Change the sheet and chart names, as necessary.

Hope this helps!
 
Upvote 0
Hi Domenic,

This did indeed help. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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