对 If/Else 使用 GoTo 语句时出错

Error when using GoTo statement for If/Else

仅当相关列中有数据时,我才使用以下代码调整图表元素的外观:

mcwb.ChartObjects("Combined RTS").Activate
k = 3
Dim c1 As Range
Dim c2 As Range

Do Until wb.Worksheets("RTS Raw Data").Cells(1, k) = ""
  Set c1 = Cells(3, k)
  Set c2 = Cells(lr, k)

  With wb.Worksheets("RTS Raw Data")
    If (.Cells(3, k) <> "-999") Then
    ActiveChart.SeriesCollection.Add Source:=.Range(.Cells(3, k), .Cells(lr, k))
        GoTo Line1
    Else
        GoTo Line2
    End If
  End With

Line1:
  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)
  ActiveChart.SeriesCollection(11 + k).AxisGroup = xlPrimary
Line2:
  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

  k = k + 1
Loop

然而,当它运行时,标签内的代码 Line1 return 不当使用错误。我的目标是减少 运行 脚本使用的指令数量和循环中花费的时间。提前致谢。

发生错误是因为您在 With 块中使用了 GoTo 并且该块被移出范围。避免使用 With 块,或避免使用 GoTo 语句(通常不被接受,除非非常特殊的用途)。

这也可以通过使用 ChartSeries 以及 Worksheet 对象来改进,这将使代码更清晰一些:)我们还可以改进嵌套if/else 条件使用 If/ElseIf/Else,您应该 使用 您的变量(例如,c1, c2 而不是通过它们的 .Cells 重复引用它们标识符 :)

Dim cht as Chart
Dim srs as Series
Dim wsRTS as Worksheet
Set cht = ActiveChart
Set wsRTS = wb.Worksheets("RTS Raw Data")

With wsRTS
    Do Until .Cells(1, k) = ""
      'If these Cells are on wsRTS, you could put the entire DO LOOP inside the WITH block.
      Set c1 = .Cells(3, k)
      Set c2 = .Cells(lr, k)

      If c1.Value <> "-999" Then
          'Use your c1,c2 range objects to define the series range!
          Set srs = cht.SeriesCollection.Add(Source:=.Range(c1, c2))
          srs.XValues = .Range("B3:B" & lr).Value
          srs.Name = .Cells(2, k)
          srs.AxisGroup = xlPrimary
      ElseIf c1.Value = "-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
      k = k + 1
    Loop
End With

如果您在执行上述内容时遇到困难,请从下面的内容开始并尝试逐个执行一些更改,以便根据上述建议清理您的代码。例如,您 Set c1 但从未使用它,即使您在代码的其他地方引用了该对象 (.Cells(3,k)) ,获得 Series 对象的句柄意味着您可以使用它而不是 ActiveChart.SeriesCollection(_index_) 这是一个笨拙的构造,您正在使用 With wb.Worksheets("RTS Raw Data") 但在 With 块 中反复明确地引用该对象 这违背了首先使用 With 块!等:

Do Until wb.Worksheets("RTS Raw Data").Cells(1, k) = ""
  Set c1 = Cells(3, k)
  Set c2 = Cells(lr, k)

  With wb.Worksheets("RTS Raw Data")
    If (.Cells(3, k) <> "-999") Then
        ActiveChart.SeriesCollection.Add Source:=.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)
        ActiveChart.SeriesCollection(11 + k).AxisGroup = xlPrimary
    Else
        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
    End If
  End With
  k = k + 1
Loop