Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Data Labels in Excel Scatterplot

Posted on 2002-07-20
6
Medium Priority
?
292 Views
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?
0
Comment
Question by:FlorisMK
6 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 200 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

 '...show 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
 '"xlsheet".
 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

HAGD:O)Bruintje
0
 
LVL 2

Author Comment

by:FlorisMK
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? (-:
0
 
LVL 44

Expert Comment

by:bruintje
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:cri
ID: 7171224
Get the Chart Labeler from http://www.bmsltd.co.uk/MVP/Default.htm . Besides you having it in Menu|Tools for all future workbooks, if it allows you to format the labels at definition time.
0
 
LVL 2

Author Comment

by:FlorisMK
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... (-:
0
 
LVL 1

Expert Comment

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

Computer101
E-E Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
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…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

972 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