继续For循环

我有以下代码

For x = LBound(arr) To UBound(arr) sname = arr(x) If instr(sname, "Configuration item") Then '**(here i want to go to next x in loop and not complete the code below)** '// other code to copy past and do various stuff Next x 

所以我想我可以简单地有语句Then Next x ,但是这给了一个“不适用于声明声明”的错误。

那么,我可以把什么后, If instr(sname, "Configuration item") Then使其继续为X的下一个值?

你不能像这样使用Next 。 您可以使用GoTo语句来实现您正在尝试执行的操作,但实际上, GoTo应该保留用于替代方法不切实际的情况。

在你的情况下,有一个非常简单,干净,可读的替代scheme:

  If Not InStr(sname, "Configuration item") Then '// other code to copy past and do various stuff End If 

您可以使用GoTo

 Do '... do stuff your loop will be doing ' skip to the end of the loop if necessary: If <condition-to-go-to-next-iteration> Then GoTo ContinueLoop '... do other stuff if the condition is not met ContinueLoop: Loop 

很多年后…我喜欢这个:

 For x = LBound(arr) To UBound(arr): Do sname = arr(x) If instr(sname, "Configuration item") Then Exit Do '// other code to copy past and do various stuff Loop While False: Next x 
 For i=1 To 10 Do 'Do everything in here and If I_Dont_Want_Finish_This_Loop Then Exit Do End If 'Of course, if I do want to finish it, 'I put more stuff here, and then... Loop While False 'quit after one loop Next i 

几年后,但这是另一种select。

 For x = LBound(arr) To UBound(arr) sname = arr(x) If InStr(sname, "Configuration item") Then 'Do nothing here, which automatically go to the next iteration Else 'Code to perform the required action End If Next x 

我有时会做一个双重循环:

 Do Do If I_Don't_Want_to_Finish_This_Loop Then Exit Do Exit Do Loop Loop Until Done 

这避免了“goto意大利面条”