【发布时间】: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