【发布时间】:2018-03-27 09:31:23
【问题描述】:
我正在尝试从该页面上的单个数据表动态地将新图表系列添加到单个图表。但是,在 .XValues 命令期间,我不断收到
运行时错误“1004”:应用程序定义的或对象定义的错误。
我正在使用以下代码:
Sub addseries()
Dim endpt1 As Range
Dim endpt2 As Range
Dim Address1 As Range
Dim Address2 As Range
For x = 2 To (Sheets.Count - 1)
Cells(1, 2 * x - 1).Select
Selection.End(xlDown).Select
Set endpt1 = ActiveCell
Cells(1, 2 * x).Select
Selection.End(xlDown).Select
Set endpt2 = ActiveCell
Range(Cells(2, 2 * x - 1), endpt1).Select
Set Address1 = Selection
Range(Cells(2, 2 * x), endpt2).Select
Set Address2 = Selection
Debug.Print ("Last Row1: " & endpt1.Address & " Last Row2: " & endpt2.Address)
Debug.Print "x Range: " & Address1.Address
Debug.Print "Value Range: " & Address2.Address
'Add new series
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
'Set ranges for new series
ActiveChart.FullSeriesCollection(x).Name = ActiveSheet.Cells(1, 2 * x)
ActiveChart.FullSeriesCollection(x).XValues = "='Merged Plot'!Address1.Address()"
ActiveChart.FullSeriesCollection(x).Values = "='Merged Plot'!Address2.Address()"
Next x
End sub
这些是立即窗口中显示的值:
Last Row1: $C$62 Last Row2: $D$62
x Range: $C$2:$C$62
Value Range: $D$2:$D$62
【问题讨论】:
-
X 从 2 开始,因此您需要减去 1 才能获得当前系列,例如ActiveChart.SeriesCollection(x - 1).Name
-
但是这是什么? "='Merged Plot'!Address1.Address()" Address1 位?