使用范围时添加数据系列失败
Add data series fails when using range
我有以下循环尝试为一系列列附加数据系列:
Do Until wb.Worksheets("RTS Raw Data").Cells(1, k) = ""
Set c1 = Cells(3, k)
Set c2 = Cells(lr, k)
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))
ActiveChart.SeriesCollection(11 + k).XValues = wb.Worksheets("RTS Raw Data").Range("B3:B" & lr)
ActiveChart.SeriesCollection(11 + k).Name = wb.Worksheets("RTS Raw Data").Cells(2, k)
If (wb.Worksheets("RTS Raw Data").Cells(3, k) = "-999") Then
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = "Unused"
Else
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = wb.Worksheets("RTS Raw Data").Cells(2, k)
End If
ActiveChart.SeriesCollection(11 + k).AxisGroup = xlPrimary
k = k + 1
Loop
代码在 ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))
上失败,但在使用 .Range("E3:E" & lr)
时不会失败。
提前致谢。
ActiveChart 似乎没有打开 RTS Raw Data
。
在您的代码中,Range
被定义为在 RTS Raw Data
上,但是两次出现的 Cells
不符合 sheet 的条件,因此它使用活动sheet.
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))
尝试使用 With...End With
块来缩短您的代码:
With wb.Worksheets("RTS Raw Data")
ActiveChart.SeriesCollection.Add Source:=.Range(.Cells(3, k), .Cells(lr, k))
End With
这与写作相同:
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(wb.Worksheets("RTS Raw Data").Cells(3, k), wb.Worksheets("RTS Raw Data").Cells(lr, k))
为了使所有对象(Range
和 Cells
)符合 "RTS Raw Data" sheet,请使用 With wb.Worksheets("RTS Raw Data")
,并将所有对象嵌套在下面使用 .
(代码也更清晰,更短)。
另外,如果你自己定义了Dim Ser As Series
,对你以后编码调试也有很大的帮助。
试试下面的代码:
Dim Ser As Series
With wb.Worksheets("RTS Raw Data")
Do Until .Cells(1, k) = ""
Set c1 = .Cells(3, k)
Set c2 = .Cells(lr, k)
Set Ser = ActiveChart.SeriesCollection.Add ' <-- set Series to a variable, easier to code and debug later
Ser.Values = .Range(.Cells(3, k), .Cells(lr, k))
Ser.XValues = .Range("B3:B" & lr)
Ser.Name = .Cells(2, k)
If .Cells(3, k) = "-999" Then
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = "Unused"
Else
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = .Cells(2, k)
End If
Ser.AxisGroup = xlPrimary
k = k + 1
Loop
End With
我有以下循环尝试为一系列列附加数据系列:
Do Until wb.Worksheets("RTS Raw Data").Cells(1, k) = ""
Set c1 = Cells(3, k)
Set c2 = Cells(lr, k)
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))
ActiveChart.SeriesCollection(11 + k).XValues = wb.Worksheets("RTS Raw Data").Range("B3:B" & lr)
ActiveChart.SeriesCollection(11 + k).Name = wb.Worksheets("RTS Raw Data").Cells(2, k)
If (wb.Worksheets("RTS Raw Data").Cells(3, k) = "-999") Then
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = "Unused"
Else
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = wb.Worksheets("RTS Raw Data").Cells(2, k)
End If
ActiveChart.SeriesCollection(11 + k).AxisGroup = xlPrimary
k = k + 1
Loop
代码在 ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))
上失败,但在使用 .Range("E3:E" & lr)
时不会失败。
提前致谢。
ActiveChart 似乎没有打开 RTS Raw Data
。
在您的代码中,Range
被定义为在 RTS Raw Data
上,但是两次出现的 Cells
不符合 sheet 的条件,因此它使用活动sheet.
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))
尝试使用 With...End With
块来缩短您的代码:
With wb.Worksheets("RTS Raw Data")
ActiveChart.SeriesCollection.Add Source:=.Range(.Cells(3, k), .Cells(lr, k))
End With
这与写作相同:
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(wb.Worksheets("RTS Raw Data").Cells(3, k), wb.Worksheets("RTS Raw Data").Cells(lr, k))
为了使所有对象(Range
和 Cells
)符合 "RTS Raw Data" sheet,请使用 With wb.Worksheets("RTS Raw Data")
,并将所有对象嵌套在下面使用 .
(代码也更清晰,更短)。
另外,如果你自己定义了Dim Ser As Series
,对你以后编码调试也有很大的帮助。
试试下面的代码:
Dim Ser As Series
With wb.Worksheets("RTS Raw Data")
Do Until .Cells(1, k) = ""
Set c1 = .Cells(3, k)
Set c2 = .Cells(lr, k)
Set Ser = ActiveChart.SeriesCollection.Add ' <-- set Series to a variable, easier to code and debug later
Ser.Values = .Range(.Cells(3, k), .Cells(lr, k))
Ser.XValues = .Range("B3:B" & lr)
Ser.Name = .Cells(2, k)
If .Cells(3, k) = "-999" Then
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = "Unused"
Else
mcwb.Shapes("CheckBox" & 8 + k).TextFrame.Characters.Text = .Cells(2, k)
End If
Ser.AxisGroup = xlPrimary
k = k + 1
Loop
End With