Excel-VBA ActiveChart.FullSeriesCollection.Name 未链接到单元格

Excel-VBA ActiveChart.FullSeriesCollection.Name not linking to cell

以下是创建新系列名称的两个示例:

ActiveChart.FullSeriesCollection(1).Name = "='GSI with DSPV Data'!$BUX" 

ActiveChart.FullSeriesCollection(1).Name = Worksheets("GSI with DSPV Data").Cells(nameRow, nameCol)
在这种情况下,

nameRownameCol 可以设置为与列:BUX 和行:4 相同的值。在这两种情况下,图例都正确显示了新系列名称。但是,第二个示例没有 link 单元格到系列名称 gui 编辑框。系列名称框保持空白。

第一个示例对我没有用,因为我需要使用列和行的变量来表示单元格,因为这是在迭代 For 循环中。

请参阅下面的完整代码:

Sub ExtendPlot()

Dim nameRow As Integer
Dim nameCol As Integer
Dim maxPlotRow As Integer
Dim xPlotCol As Integer
Dim minPlotRow As Integer
Dim yPlotCol As Integer
Dim xValues As Range
Dim yValues As Range

nameRow = 4 'the series name is always on the same row
minPlotRow = 2 'the minimum plotted row is always 2
maxPlotRow = 400 'the maximum plotted row is always 400
nameCol = Sheets("GSI with DSPV Data").Columns("BUX").Column 'specify the column of the series name location and turn this into an integer
xPlotCol = Sheets("GSI with DSPV Data").Columns("BUY").Column 'specify the column of the series x values location and turn this into an integer
yPlotCol = Sheets("GSI with DSPV Data").Columns("BUZ").Column 'specify the column of the series y values location and turn this into an integer
Set xValues = Range(Sheets("GSI with DSPV Data").Cells(minPlotRow, xPlotCol), Sheets("GSI with DSPV Data").Cells(maxPlotRow, xPlotCol)) 'set the range of the x axis given the above values
Set yValues = Range(Sheets("GSI with DSPV Data").Cells(minPlotRow, yPlotCol), Sheets("GSI with DSPV Data").Cells(maxPlotRow, yPlotCol)) 'set the range of the y axis given the above values
ActiveChart.SeriesCollection.NewSeries 'create a new series on the current graph
ActiveChart.FullSeriesCollection(1).Name = "='GSI with DSPV Data'!$BUX" 'create the name for the new series by linking to the reference cell
ActiveChart.FullSeriesCollection(1).xValues = xValues 'create the x axis by linking to the x axis range
ActiveChart.FullSeriesCollection(1).Values = yValues 'create the y axis by linking to the y axis range

For i = 2 To 30 'start a For loop for the next coming series
nameCol = nameCol + 8 'the name of the next series is always located 8 columns later than the first
xPlotCol = xPlotCol + 8 'the x axis values of the next series are always located 8 columns later than the first
yPlotCol = yPlotCol + 8 'the y axis values of the next series are always located 8 columns later than the first
Set xValues = Range(Sheets("GSI with DSPV Data").Cells(minPlotRow, xPlotCol), Sheets("GSI with DSPV Data").Cells(maxPlotRow, xPlotCol)) 'set the range of the x axis given the above values
Set yValues = Range(Sheets("GSI with DSPV Data").Cells(minPlotRow, yPlotCol), Sheets("GSI with DSPV Data").Cells(maxPlotRow, yPlotCol)) 'set the range of the y axis given the above values

ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(i).Name = Worksheets("GSI with DSPV Data").Cells(nameRow, nameCol) 'create the name for the new series by linking to the reference cell
ActiveChart.FullSeriesCollection(i).xValues = xValues 'create the x axis by linking to the x axis range
ActiveChart.FullSeriesCollection(i).Values = yValues 'create the y axis by linking to the y axis range
Next i

End Sub

我特意为 x 轴和 y 轴使用了不同的范围,这样零 VBA 经验的人也能准确地描绘出这里发生的事情。这样做并不是计算详尽(我不认为?)。就是看起来不太整齐。

试试下面的方法怎么样:

Dim nameRow As Long, NameCol As String
Dim SerTitleNameRng As Range

NameCol = "BUX"
nameRow = 4

With Worksheets("GSI with DSPV Data")
    Set SerTitleNameRng = .Cells(nameRow, NameCol)
End With

ActiveChart.FullSeriesCollection(1).Name = "=" & SerTitleNameRng.Address(True, True, xlA1, xlExternal)