VBA - Auto label graph with data labels

joek128

New Member
Joined
Jul 22, 2016
Messages
23
Hello,

Looking for a way to fix labels to dynamic chart objects. The example below:

Two dynamic series (the red bubbles and green bubbles) with the size representing the contribution to the portfolio. Looking to write some form of code that will take the Ticker Symbols from Column A (for data in Series A - Green Bubbles) and Column B (for data in Series B - Red Bubbles)and auto update the labels to each series. Any idea how to do this? Thanks!

http://imgur.com/XZiIJma
XZiIJma.png
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I haven't looked at what you have in your link, but hopefully this will do what you want. Note that it doesn't include any error handling, and that it's based on the following assumptions...

1) Sheet1, Column A and B, starting at Row 2, contain the ticker symbols to be used for the data labels.

2) Sheet2 contains the chart, and the chart is named "Chart 1".

3) The labels are to be position below each data point.

Code:
Option Explicit

Sub AddLabelsToChart()

    Dim rLabels As Range
    Dim LastRow As Long
    Dim SeriesIndex As Long
    Dim PointIndex As Long
    
    With Worksheets("Sheet1")
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        If LastRow < 2 Then
            MsgBox "No data is available.", vbInformation
            Exit Sub
        End If
        Set rLabels = .Range("A2:B" & LastRow)
    End With
    
    With Worksheets("Sheet2").ChartObjects("Chart 1").Chart
        For SeriesIndex = 1 To 2
            With .SeriesCollection(SeriesIndex)
                For PointIndex = 1 To .Points.Count
                    With .Points(PointIndex)
                        .HasDataLabel = True
                        With .DataLabel
                            .Text = rLabels.Cells(PointIndex, SeriesIndex).Text
                            .Position = xlLabelPositionBelow
                        End With
                    End With
                Next PointIndex
            End With
        Next SeriesIndex
    End With
    
    MsgBox "Labels have been added.", vbInformation
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,196,483
Messages
6,015,461
Members
441,898
Latest member
kofafa

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