继续 For 循环 [英] Continue For loop
问题描述
我有以下代码
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
,但这给出了没有声明声明"错误.
So I thought I could simply have the statement Then Next x
, but this gives a "no for statement declared" error.
那么我可以在 If instr(sname, "Configuration item") Then
后面放什么来使它继续到 x 的下一个值?
So what can I put after the If instr(sname, "Configuration item") Then
to make it proceed to the next value for x?
推荐答案
您正在考虑使用 continue
语句,例如 Java 的 或 Python 的,但是 VBA 没有这样的原生语句,你不能像那样使用 VBA 的 Next
.
You're thinking of a continue
statement like Java's or Python's, but VBA has no such native statement, and you can't use VBA's Next
like that.
您可以使用 GoTo
语句来实现类似您想要做的事情,但实际上,GoTo
应该保留用于替代方案被人为设计和不切实际的.
You could achieve something like what you're trying to do using a GoTo
statement instead, but really, GoTo
should be reserved for cases where the alternatives are contrived and impractical.
对于单个继续"条件的情况,有一个非常简单、干净且可读的替代方案:
In your case with a single "continue" condition, there's a really simple, clean, and readable alternative:
If Not InStr(sname, "Configuration item") Then
'// other code to copy paste and do various stuff
End If
这篇关于继续 For 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!