這是一個相當奇怪的問題,在Excel 的散佈圖中,無論是在2003或是2007版本,竟然無法顯示散佈圖之資料標籤。最近找了一些資料才發現,必須手動方式增加。

1.首先在巨集底下新增一個模組

2.將以下程式碼貼上後,點選執行

3.這樣散佈圖便會自動增加資料標籤

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

arrow
arrow
    全站熱搜

    yrli 發表在 痞客邦 留言(1) 人氣()