Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to use a macro to add labels to data points in an xy scatter chart or in a bubble chart in Excel


View products that this article applies to.

Summary

In Microsoft Excel, there is no built-in command that automatically attaches text labels to data points in an xy (scatter) or Bubble chart. However, you can create a Microsoft Visual Basic for Applications macro that does this. This article contains a sample macro that performs this task on an XY Scatter chart. However, the same code can be used for a Bubble Chart.

↑ Back to the top


More Information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The sample code in this article assumes that the data and associated labels are arranged in a worksheet according to the following format:
  • The first column contains the data labels.
  • The second column contains the x values for the xy (scatter) chart.
  • The third and subsequent columns contain the y values for the xy (scatter) chart.
NOTE: Although the example provided contains only one column of data for y values, you can use more than one series of data.

To use the macros in this article, create a chart using the following data:
   A1: Labels       B1: X Values   C1: Y Values
   A2: DataPoint1   B2: 12          C2: 5
   A3: DataPoint2   B3:  9          C3: 7
   A4: DataPoint3   B4:  5          C4: 3
   A5: DataPoint4   B5:  4          C5: 8
   A6: DataPoint5   B6:  1          C6: 4 
NOTE: The table should not contain empty columns, and the column that contains the data labels should not be separated from the column that contains the x values. The labels and values must be laid out in exactly the format described in this article. (The upper-left cell does not have to be cell A1.)

To attach text labels to data points in an xy (scatter) chart, follow these steps:
  1. On the worksheet that contains the sample data, select the cell range B1:C6.
  2. In Microsoft Office Excel 2003 and in earlier versions of Excel, follow these steps:
    1. Click Chart on the Insert menu.
    2. In the Chart Wizard - Step 1 of 4 - Chart Type dialog box, click the Standard Types tab. Under Chart type, click XY (Scatter), and then click Next.
    3. In the Chart Wizard - Step 2 of 4 - Chart Source Data dialog box, click the Data Range tab. Under Series in, click Columns, and then click Next.
    4. In the Chart Wizard - Step 3 of 4 - Chart Options dialog box, click Next.
    5. In the Chart Wizard - Step 4 of 4 -Chart Location dialog box, click the As new sheet option, and then click Finish.
    In Microsoft Office Excel 2007, follow these steps:
    1. Click the Insert tab, click Scatter in the Charts group, and then select a type.
    2. On the Design tab, click Move Chart in the Location group, click New sheet , and then click OK.
  3. Press ALT+F11 to start the Visual Basic Editor.
  4. On the Insert menu, click Module.
  5. Type the following sample code in the module sheet:
    Sub AttachLabelsToPoints()
    
       'Dimension variables.
       Dim Counter As Integer, ChartName As String, xVals As String
    
       ' Disable screen updating while the subroutine is run.
       Application.ScreenUpdating = False
    
       'Store the formula for the first series in "xVals".
       xVals = ActiveChart.SeriesCollection(1).Formula
    
       'Extract the range for the data from xVals.
       xVals = Mid(xVals, InStr(InStr(xVals, ","), xVals, _
          Mid(Left(xVals, InStr(xVals, "!") - 1), 9)))
       xVals = Left(xVals, InStr(InStr(xVals, "!"), xVals, ",") - 1)
       Do While Left(xVals, 1) = ","
          xVals = Mid(xVals, 2)
       Loop
    
       'Attach a label to each data point in the chart.
       For Counter = 1 To Range(xVals).Cells.Count
         ActiveChart.SeriesCollection(1).Points(Counter).HasDataLabel = _
             True
          ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text = _
             Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
       Next Counter
    
    End Sub
  6. Press ALT+Q to return to Excel.
  7. Switch to the chart sheet.
  8. In Excel 2003 and in earlier versions of Excel, point to Macro on the Tools menu, and then click Macros. Click AttachLabelsToPoints, and then click Run to run the macro.

    In Excel 2007, click the Developer tab, click Macro in the Code group, select AttachLabelsToPoints, and then click Run.
The macro attaches the labels in cells A2:A6 to the data points on the chart.

↑ Back to the top


Keywords: offcon, kbprogramming, kbinfo, kbhowto, kbdtacode, kbchart, kboffice2003yes, kb, kbsweptsoltax, kbfreshness2006, kboffice12yes, epucon

↑ Back to the top

Article Info
Article ID : 213750
Revision : 4
Created on : 4/17/2018
Published on : 4/17/2018
Exists online : False
Views : 436