我試圖製作一個宏,它將將散點圖上的數據標籤更改爲每個點的標題。例如:在VBA中使用動態命名範圍進行散點圖標記
Example chart http://www.wiseowl.co.uk/blog/s130/i2.gif
在這種情況下,我想數據標籤,以顯示電影標題。我使用的代碼來自this website,但我的散點圖有兩個系列的數據。我想使用動態命名範圍來告訴宏名稱所在單元格的範圍,在我的代碼中,動態命名範圍是FONames & BONames。這裏是我的代碼:
Sub CreateDataLabels()
'holds the entire film data series in the chart
Dim FilmDataSeries As Series
Dim FilmDataSeries2 As Series
'holds one cell at a time
Dim SingleCell As Range
'holds the full list of cells containing film names
Dim FilmList As Range
Dim FilmList2 As Range
'keeps track of which datapoint we're labelling
Dim FilmCounter As Integer
Dim FilmCounter2 As Integer
'The sheet in which the graph is on
Dim ws As Worksheet
Set ws = Worksheets("Datos Gráfico")
'set the counter to start at 1
FilmCounter = 1
FilmCounter2 = 1
'set a reference to the cells containing the list of films
Set FilmList = Range("FONames")
Set FilmList2 = Range("BONames")
'set a reference to the chart data series
Set FilmDataSeries = ws.ChartObjects(1).Chart.SeriesCollection(1)
Set FilmDataSeries2 = ws.ChartObjects(1).Chart.SeriesCollection(2)
'make sure data labels are turned on
FilmDataSeries.HasDataLabels = True
FilmDataSeries2.HasDataLabels = True
'loop over the cells in the list of films
For Each SingleCell In FilmList
FilmDataSeries.Points(FilmCounter).DataLabel.Text = SingleCell.Value
FilmCounter = FilmCounter + 1
Next SingleCell
For Each SingleCell In FilmList2
FilmDataSeries2.Points(FilmCounter2).DataLabel.Text = SingleCell.Value
FilmCounter2 = FilmCounter2 + 1
Next SingleCell
End Sub
我得到一個1004錯誤,說明指定的維度無效。當我用實際範圍的單元格替換FONames或BONames時,例如。範圍(「A5」,「A11」),一切正常。我如何在代碼中使用這些動態命名範圍?
已定義的命名範圍,當你運行該代碼?代碼的命名範圍設置位適合我。 –