简体   繁体   English

使用范围时添加数据系列失败

[英]Add data series fails when using range

I have the following loop trying to append data series for a range of columns: 我有以下循环尝试为一系列列追加数据系列:

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

The code fails on ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k)) but does not fail when .Range("E3:E" & lr) is used. 代码在ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))代码上失败ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))但在.Range("E3:E" & lr)

Thanks in advance. 提前致谢。

It looks like the ActiveChart isn't on RTS Raw Data . 看来ActiveChart不在RTS Raw Data

In your code the Range is defined as being on RTS Raw Data , but the two occurrences of Cells isn't qualified to a sheet so it's using the activesheet. 在您的代码中, Range被定义为位于RTS Raw Data ,但是两次出现的Cells都不符合工作表的条件,因此它使用的是活动工作表。
ActiveChart.SeriesCollection.Add Source:=wb.Worksheets("RTS Raw Data").Range(Cells(3, k), Cells(lr, k))

Try using a With...End With block to shorten your code a bit: 尝试使用With...End With块来缩短代码:

With wb.Worksheets("RTS Raw Data")
    ActiveChart.SeriesCollection.Add Source:=.Range(.Cells(3, k), .Cells(lr, k))
End With

This is the same as writing: 这与写作相同:

 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))  

https://msdn.microsoft.com/en-us/library/wc500chb.aspx https://msdn.microsoft.com/zh-CN/library/wc500chb.aspx

In order to have all objects ( Range and Cells ) qualified with the "RTS Raw Data" sheet, use With wb.Worksheets("RTS Raw Data") , and nest all objects underneath with a . 为了使所有对象( RangeCells )都符合“ RTS Raw Data”表的要求,请With wb.Worksheets("RTS Raw Data") ,并将所有对象嵌套在下面. (the code also much clearer to the eye, and shorter). (代码也更清晰,更短)。

Also, if you define Dim Ser As Series , it will also help you a lot to code and debug it later. 另外,如果您定义Dim Ser As Series ,那么在以后进行编码和调试时也会有很大帮助。

Try the code below: 请尝试以下代码:

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

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

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