【问题标题】:other method(s) for code to go back to the procedure?代码返回程序的其他方法?
【发布时间】:2014-02-12 16:39:46
【问题描述】:

背景:

我试图为一个巨大的电子表格构建一个错误(查找拼写错误等)指示器,根据不同的标准在每一列中发现错误,但通常代码的每个部分(程序?)使用如果发现错误,则循环 if 语句,代码将转到 (goto) 错误列表,这是一个应该将错误及其单元格制成表格的过程。

问题:

Goto 不会像我想象的那样运行,最后的巨大 if 语句不会返回到代码中,并且代码在第一个错误被列表后停止运行,可以做些什么来解决这个问题,因为我想要代码回到程序,我确定我的编码是问题所在,并且 goto 语句能够在代码中出现,这就是为什么我会提到整个冗长且老实说是混乱的代码,这样如果有人能抓住问题。 请关注最后的 if 语句,因为这是问题所在

code: (only a sample including one procedure of many others, this one checks column L for errors)

Sub errorinsight()

    Sheets("sheet1").Activate
    Dim ucolumn As String



    Dim i As Long, j As Long, r As Range
        ' if your data is in a different column then change L to some other letter(s)
ucolumn = "L" 'pick up contr.


For i = 2 To Range(ucolumn & Rows.Count).End(xlUp).Row
        Set r = Range(ucolumn & i)
        If r = MAXXAM Or r = SGS Then
        GoTo nexti1
        Else

        GoTo errorlist
        End If
complink:
nexti1:
        Next i




GoTo mastercodeend

errorlist:

Sheets.Add.Name = "errorsheet" & Format(Now, "yyyy_mm_dd ss_nn_hh")

    Dim counter As Integer, xerror As Range, yerror As Range

    counter = 1
    Set xerror = Range("a" & counter)
    xerror = ucolumn & i
     Set yerror = Range("b" & counter)
     yerror = r
     counter = counter + 1
      ElseIf ucolumn = "L" Then
     GoTo complink
         Else
         End If
    mastercodeend:
    End Sub

【问题讨论】:

  • 如果你在错误处理程序中完成了 something ,而不是在处理程序的末尾粘贴 resume next ,它应该回到原来的位置
  • 请原谅我的无知,但我认为您的回答与此无关,首先抱歉这个混乱的问题
  • 对不起,我可能误解了你的问题,但这就是我看到你的问题的方式what can be done to solve this problem as i want the code to go back to the procedure
  • 我不建议使用 Goto,因为它总是会导致问题...只需调用一个您将使用 goto 的函数,然后在该函数中编写 goto 代码...

标签: loops excel if-statement goto vba


【解决方案1】:

最好的办法是使用一些子例程和/或函数来划分代码。正如您所观察到的,使用或过度使用 GoTo 语句会变得一团糟。

Sub errorinsight()
    Dim i As Long, j As Long, r As Range
    Dim ucolumn As String
    Dim counter As Integer: counter = 0

    Sheets("sheet1").Activate

    'if your data is in a different column then change L to some other letter(s)
    ucolumn = "L" 'pick up contr.

    For i = 2 To Range(ucolumn & Rows.Count).End(xlUp).Row
        Set r = Range(ucolumn & i)
        If Not r = MAXXAM AND Not r = SGS Then
           FlagErrors ucolumn, r, counter + 1
        End If
    Next i
End Sub

Sub FlagErrors(ucolumn as String, i as Long, r as Range, byRef counter as Integer)
    Dim xerror As Range, yerror As Range
    Sheets.Add.Name = "errorsheet" & Format(Now, "yyyy_mm_dd ss_nn_hh")
    Range("A" & counter) = ucolumn & i
    Range("B" & counter) = r
    Sheets("sheet1").Activate

    'I remove this block because it doesn't do anything.
    '  * uColumn is hardcoded as "L" in your procedure above
    '  * samplecodelink is an undefined label in your procedure and will raise an error
    '  * GoTo compLink is unnecessary since this sub will return to the next line in the calling procedure
     'If ucolumn = "A" Then
     'GoTo samplecodelink
     '     ElseIf ucolumn = "L" Then
     'GoTo complink
     '    Else
     '    End If

End Sub

【讨论】:

  • 大卫,这只是我的代码示例,因为它太长了,问题只出现在最后一部分(连接除 L 以外的不同列的所有检测程序),我会尝试使用您建议的方法,您的答案很受欢迎。
  • 我在第一个 sub 中收到 FlagErrors 的错误消息“编译错误:参数不是可选的”
  • FlagErrors ucolumn, r, counter + 1(我忘了r
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
相关资源
最近更新 更多