简体   繁体   English

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

[英]Excel-VBA ActiveChart.FullSeriesCollection.Name not linking to cell

Below are two examples for creating a name of a new series: 下面是创建新系列名称的两个示例:

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

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

nameRow and nameCol can be set to the same values as column: BUX and row: 4 in this instance. 在这种情况下,可以将nameRownameCol设置为与column:BUX和row:4相同的值。 In both cases, the legend correctly displays the new series name. 在两种情况下,图例均正确显示新的系列名称。 However, the second example doesn't link the cell to the series name gui edit box. 但是,第二个示例未将单元格链接到系列名称gui编辑框。 The series name box remains blank. 系列名称框保持空白。

The first example is not useful to me as I need to represent the cell using variables for the column and row, as this is within an iterative For loop. 第一个示例对我没有用,因为我需要使用用于列和行的变量表示单元格,因为这在迭代For循环中。

Please see below for my full code: 请参阅下面的完整代码:

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

I have used separate ranges for the x axis and y axis on purpose, such that people with zero VBA experience can picture exactly what's going on here. 我特意为x轴和y轴使用了单独的范围,以便VBA经验为零的人们可以准确地了解这里发生的情况。 It is not that computational exhaustive (I don't think?) to do this. 做到这一点并不是计算上的详尽无遗(我不认为吗?)。 It just doesn't look very neat. 看起来并不整洁。

How about trying it in the method below: 如何通过以下方法尝试:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM