Вы находитесь на странице: 1из 2

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:
6. Click the Insert tab, click Scatter in the Charts group, and then select a
type.
7. 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:
6.
7. Sub AttachLabelsToPoints()
8.
9.
'Dimension variables.
10.
Dim Counter As Integer, ChartName As String, xVals As String
11.
12.
' Disable screen updating while the subroutine is run.
13.
Application.ScreenUpdating = False
14.
15.
'Store the formula for the first series in "xVals".
16.
xVals = ActiveChart.SeriesCollection(1).Formula
17.
18.
'Extract the range for the data from xVals.

19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

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

29.
30.

ActiveChart.SeriesCollection(1).Points(Counter).DataLabel.Text = _
Range(xVals).Cells(Counter, 1).Offset(0, -1).Value
Next Counter

31.
32.
33.

End Sub

34. Press ALT+Q to return to Excel.


35. Switch to the chart sheet.
36. 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.

Вам также может понравиться