Solved

Data Labels in Excel Scatterplot

Posted on 2002-07-20
6
242 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 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

 '...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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing causes application to got into "not responding" 6 43
MS Office 2013 for purchase 3 56
Save Email Attachments using VBA Excel 3 61
OCT or Config.xml 2 41
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now