Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Using Excel VBA to Label Multiple Series in a Chart |
---|
This follow-up article to a previous blog describes how to label the data points in multiple series in Excel using VBA. |
In this blog
A while ago I wrote a blog describing how you could use VBA in Excel to label the data points in a scatter chart. Recently someone asked a question about how to adapt the code I created to label more than one series in the same chart. Here's the problem we're trying to solve:
The chart contains one data series plotting RunTime against Budget and another plotting RunTime against Box Office. We want to label each data point with the name of the film.
The key to this solution is looping over each data series for every data point that we want to label. The code below will work for any number of data series that the chart contains:
Sub CreateDataLabels()
'variables for looping over chart objects
Dim FilmChart As Chart
Dim FilmDataSeries As Series
'variables for looping over cells
Dim SingleCell As Range
Dim FilmList As Range
'variable to keep track of number of films
Dim FilmCounter As Integer
FilmCounter = 1
Set FilmList = Range("A2", "A11")
Set FilmChart = ActiveSheet.ChartObjects(1).Chart
'loop over each data series and enable data labels
For Each FilmDataSeries In FilmChart.SeriesCollection
FilmDataSeries.HasDataLabels = True
Next FilmDataSeries
'loop over each cell in the list of source data
For Each SingleCell In FilmList
'loop over each series in the chart
For Each FilmDataSeries In FilmChart.SeriesCollection
'change the label text to be the film's name
FilmDataSeries.Points(FilmCounter).DataLabel.Text = SingleCell.Value
Next FilmDataSeries
FilmCounter = FilmCounter + 1
Next SingleCell
End Sub
The end result of running this code is that each data point in the chart will be labelled:
Each data point in the chart now shows the name of the film that it represents.
You can download the completed example for this blog.
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.