gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:04:20 24 4
gpt4 key购买 nike

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

Example chart
(来源:wiseowl.co.uk)

在这种情况下,我希望数据标签显示电影标题。我使用的代码来自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 时,例如。 Range("A5","A11"),一切正常。如何在我的代码中使用这些动态命名范围?

最佳答案

动态命名范围的定义是什么?我像这样为 A 列定义了一个命名范围:

=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!$A:$A)-1,1)

它工作得很好。

关于vba - 在 VBA 中使用动态命名范围进行散点图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13382616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com