Excel-VBA ActiveChart.FullSeriesCollection.Name, не связанный с ячейкой

Ниже приведены два примера создания названия новой серии:

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

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

nameRow и nameCol могут быть установлены на те же значения, что и столбец: BUX и строка: 4 в этом случае. В обоих случаях легенда правильно отображает название новой серии. Однако во втором примере ячейка не связывается с полем редактирования графического интерфейса имени серии. Поле имени серии остается пустым.

Первый пример мне бесполезен, так как мне нужно представить ячейку, используя переменные для столбца и строки, так как это внутри итеративного цикла 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 могли точно представить, что здесь происходит. Это не настолько вычислительно исчерпывающе (я не думаю?), чтобы сделать это. Просто выглядит не очень аккуратно.


person Michael Whitaker    schedule 01.11.2017    source источник


Ответы (1)


Как насчет того, чтобы попробовать это в методе ниже:

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)
person Shai Rado    schedule 01.11.2017
comment
Огромное спасибо за помощь. Я реализовал то, что вы предложили, и это сработало. Одна вещь, однако, я не вижу необходимости в цикле With? Какой в ​​этом смысл? - person Michael Whitaker; 01.11.2017
comment
@MichaelWhitaker просто мой стиль кодирования, от него можно избавиться - person Shai Rado; 01.11.2017
comment
Достаточно честно. Большое спасибо за помощь Re this. Вы хоть представляете, зачем это нужно? Например, почему: ActiveChart.FullSeriesCollection(i).Name = Worksheets(GSI с данными DSPV).Cells(nameRow, nameCol) не работает? Я помню, как мне сказали, что этот тип команды не выбирает заголовок и, следовательно, не работает. Однако я не совсем уверен, что это означает? - person Michael Whitaker; 01.11.2017