Data Labels in Excel Scatterplot

Posted on 2002-07-20
Last Modified: 2007-12-19
I have a workbook with the X values in column A and the Y values in column B. Based on these values I have created a scatterplot of the data. Now, in column C I have the labels that I want to go with the data points. I want to get Excel to place these column C labels next to each point. But with the data labels option I can only select 'none', 'value' or 'data'. The first option is clear, the second gives me the Y value, and the third one gives me the X value as label next to the point. Is there a way to get the column C text as labels with my points?
Question by:FlorisMK
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 44

Accepted Solution

bruintje earned 50 total points
ID: 7166863
Hi FlorisMK,

grabbed this from another thread


Copy the macro below and paste it into a VBA module.
To run the macro, set up your data with the name of the data point before the x data (so instead of column C use column A for the data labels), select the data series on your plot, and run the macro.

Sub AttachLabelsToPoints()
 'Dimension variables.
 Dim xLabel, yValue As Variant
 Dim Counter As Integer, ChartName As Variant
 Dim SourceWorksheet As Variant, xvals As Variant, xcell As Variant
 'NAVAIR Changes
 Dim SeriesString As String, StringElement As String
 Dim PlotOrder As Integer, n As Integer, nchars As Integer
 SeriesString = ExecuteExcel4Macro("Get.Formula(Selection())")
 nchars = Len(SeriesString)
 For n = nchars To 1 Step -1
    StringElement = Mid(SeriesString, n, 1)
    If StringElement = "," Then Exit For
 Next n
 PlotOrder = Left(Right(SeriesString, nchars - n), nchars - n - 1)
 'End NAVAIR changes
 'Disable screen updating while  the  subroutine  runs.
 Application.ScreenUpdating = False
 'Store the name of the active  chart  in  "ChartName".
 ChartName = ActiveChart.Name

 'Store the definition of the first series in "xVals".
 xvals = ActiveChart.SeriesCollection(PlotOrder).Formula

 'These lines extract the name of the source worksheet from xvals.
 SourceWorksheet = Left(xvals, InStr(1, xvals, "!") - 1)
 SourceWorksheet = Right(SourceWorksheet, Len(SourceWorksheet) - _
 InStr(1, SourceWorksheet, "("))
 If Left(SourceWorksheet, 1) = "," Then
 SourceWorksheet = Right(SourceWorksheet, Len(SourceWorksheet) - 1)
 End If

 'Replace the actual source worksheet name with "xlsheet" so that the
 'searches that follow will work correctly if the worksheet name
 'contains commas.
 xvals = Application.Substitute(xvals, SourceWorksheet, "xlsheet")

 'More processing of the  xvals  name.
 xvals = Right(xvals, Len(xvals) - InStr(1, xvals, ","))

 'If the chart is using "assumed" x-values ...
 If Left(xvals, 1) = "," Then

 ' an alert message.  NOTE: the following two lines should be
 'entered as a single line.
 MsgBox "This xy (scatter) chart is using assumed x values." & " The macro
cannot continue."

 'Exit the subroutine if "assumed" x values are in use.
 Exit Sub
 End If

 'More processing of the  xvals  name.
 xvals = Left(xvals, InStr(1, xvals, ",") - 1)

 'Put the original source worksheet name back into xvals, replacing
 xvals = Application.Substitute(xvals, "xlsheet", SourceWorksheet)

 'Initialize a counter.
 Counter = 1

 'For each cell in the range xvals ...
 For Each xcell In Range(xvals)
 'Get the value of the label next to the current x-value.
 xLabel = xcell.Offset(0, -1).Value
 yValue = xcell.Offset(0, 1).Value
 'Check if cell has a value
 If xLabel <> "" Then
 If yValue <> "" Then
 'Attach a label to the current data point in the chart.  Note:
 'enter the following two lines as a single line.
 ActiveChart.SeriesCollection(PlotOrder).Points(Counter). _
 HasDataLabel = True
 'Put the text ("DataPointl", for example) into the attached label.
 'Note: enter the following two lines as a single line.
 ActiveChart.SeriesCollection(PlotOrder). _
 Points(Counter).DataLabel.Text = xLabel
 End If
 End If
 'Increment the counter.
 Counter = Counter + 1
 Next xcell                'loop until all done
 Application.ScreenUpdating = True
 'Make sure nothing in the chart is selected.
 Application.ExecuteExcel4Macro "SELECT("""")"
 End Sub


be carefull didn't test it yet


Author Comment

ID: 7167776
It works! It even has some superfluous XL4-macro running code, but in general it works like a charm. So do you get the points or whoever answered the first thread? (-:
LVL 44

Expert Comment

ID: 7170222
uh think that is not possible the first thread was on a user group on the internet :)

so i will gladly take care of them
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Expert Comment

ID: 7171224
Get the Chart Labeler from . Besides you having it in Menu|Tools for all future workbooks, if it allows you to format the labels at definition time.

Author Comment

ID: 7172627
Just in time, cri! The code works well, so points for that, but a nifty addin with menu command is even better, so you are stealing half Bruintjes points for this.

I'll see to a points split through CS - thanks on behalf of my friend who asked the question of me and now thinks I'm her hero... (-:

Expert Comment

ID: 7172910
Points reduced for split.  Comment from expert accepted as ansswer

E-E Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question