Solved

Data Labels in Excel Scatterplot

Posted on 2002-07-20
6
241 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 13

Expert Comment

by:cri
Comment Utility
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
Comment Utility
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
Comment Utility
Points reduced for split.  Comment from expert accepted as ansswer

Computer101
E-E Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Outlook Free & Paid Tools
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

10 Experts available now in Live!

Get 1:1 Help Now