在VBA中使用动态命名范围进行散点图标记

在VBA中使用动态命名范围进行散点图标记,vba,dynamic,label,scatter,named,Vba,Dynamic,Label,Scatter,Named,我试图制作一个宏,将散点图上的数据标签更改为每个点的标题。例如: (来源:) 在本例中,我希望数据标签显示电影标题。我使用的代码来自,但我的散点图有两个系列的数据。我想使用动态命名范围告诉宏名称所在的单元格范围,在我的代码中,动态命名范围是FONames&BONames。这是我的密码: Sub CreateDataLabels() 'holds the entire film data series in the chart Dim FilmDataSeries As Series Dim F

我试图制作一个宏,将散点图上的数据标签更改为每个点的标题。例如:


(来源:)

在本例中,我希望数据标签显示电影标题。我使用的代码来自,但我的散点图有两个系列的数据。我想使用动态命名范围告诉宏名称所在的单元格范围,在我的代码中,动态命名范围是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错误,表示指定的维度无效。当我用一个实际的单元格范围(如“A5”、“A11”)替换FONames或BONames时,一切都能正常工作。如何在代码中使用这些动态命名范围?

动态命名范围的定义是什么?我为列a定义了一个命名范围,如下所示:

=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!$A:$A)-1,1)
而且效果很好。

例如:

'---------------------------------------------------------------------------
'以便在散点图中添加标签。您必须先选择一个图形,然后才能选择。
“@param void
'---------------------------------------------------------------------------
在图()上添加子标签
作为整数的Dim计数器
作为字符串的Dim XVAL
作为整数的Dim响应
如果ActiveChart什么都不是,那么
回复=MsgBox(“注意,你的简历是什么?”,请用感叹词“信息”)
出口接头
如果结束
Application.ScreenUpdating=False
在库存和可变XVAL的范围内进行“Récupération de la range des donées que l”。
xVAL=ActiveChart.SeriesCollection(1).公式
xVAL=Mid(xVAL,InStr(InStr(xVAL,“,”),xVAL,Mid(左(xVAL,InStr(xVAL,“!”)-1),9)))
xVAL=左(xVAL,InStr)(InStr(xVAL,“!”,xVAL,“,”)-1)
左行时执行(xVAL,1)=“
xVAL=Mid(xVAL,2)
环
“一个标签上的文字点”
对于计数器=1到范围(xVals).Cells.Count
ActiveChart.SeriesCollection(1).Points(计数器).HasDataLabel=True
ActiveChart.SeriesCollection(1).点(计数器).数据标签.text=范围(xVAL).单元格(计数器,1).偏移量(0,-1).值
下一个柜台
Application.ScreenUpdating=True
端接头

运行代码时是否已定义命名范围?代码的命名范围设置位对我来说运行良好。