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

时间:2017-11-01 11:30:28

标签: excel vba excel-vba excel-2016

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

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

ActiveChart.FullSeriesCollection(1).Name = Worksheets("GSI with DSPV Data").Cells(nameRow, nameCol)

nameRownameCol可以设置为与列相同的值:BUX和row:4。在这两种情况下,图例都会正确显示新系列名称。但是,第二个示例不将单元格链接到系列名称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$4" '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经验为零的人可以准确地描绘出这里发生了什么。要做到这一点,这不是计算上的详尽(我不这么认为?)。它看起来不是很整洁。

1 个答案:

答案 0 :(得分:0)

如何在以下方法中尝试:

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)