我有以下代码
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
,但这会给出“no for statement声明”错误。
那么我可以在If instr(sname, "Configuration item") Then
后放置什么来使其继续x的下一个值?
答案 0 :(得分:79)
您可以使用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
答案 1 :(得分:43)
您正在考虑continue
语句,如Java's或Python's,但VBA没有此类原生语句,您不能像这样使用VBA Next
。
您可以使用GoTo
语句来实现类似于您尝试做的事情,但实际上,GoTo
应该保留用于替代方案设计和不切实际的情况。
在单个“继续”条件的情况下,有一个非常简单,干净和可读的替代方案:
If Not InStr(sname, "Configuration item") Then
'// other code to copy paste and do various stuff
End If
答案 2 :(得分:17)
很多年后...我喜欢这个:
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
答案 3 :(得分:12)
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
答案 4 :(得分:6)
迟了几年,但这是另一种选择。
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
答案 5 :(得分:1)
您可以通过简单的方式做到这一点,只需将 for 循环中使用的变量值更改为结束值,如示例所示
Sub TEST_ONLY()
For i = 1 To 10
ActiveSheet.Cells(i, 1).Value = i
If i = 5 Then
i = 10
End If
Next i
End Sub
答案 6 :(得分:0)
这也可以使用布尔值来解决。
For Each rngCol In rngAll.Columns
doCol = False '<==== Resets to False at top of each column
For Each cell In Selection
If cell.row = 1 Then
If thisColumnShouldBeProcessed Then doCol = True
End If
If doCol Then
'Do what you want to do to each cell in this column
End If
Next cell
Next rngCol
例如,下面是完整的示例:
(1)确定工作表上已用单元格的范围
(2)遍历每一列
(3)如果列标题是可接受的标题,则遍历列中的所有单元格
Sub HowToSkipForLoopIfConditionNotMet()
Dim rngCol, rngAll, cell As Range, cnt As Long, doCol, cellValType As Boolean
Set rngAll = Range("A1").CurrentRegion
'MsgBox R.Address(0, 0), , "All data"
cnt = 0
For Each rngCol In rngAll.Columns
rngCol.Select
doCol = False
For Each cell In Selection
If cell.row = 1 Then
If cell.Value = "AnAllowedColumnTitle" Then doCol = True
End If
If doCol Then '<============== THIS LINE ==========
cnt = cnt + 1
Debug.Print ("[" & cell.Value & "]" & " / " & cell.Address & " / " & cell.Column & " / " & cell.row)
If cnt > 5 Then End '<=== NOT NEEDED. Just prevents too much demo output.
End If
Next cell
Next rngCol
End Sub
注意:如果您没有立即抓住它,则If docol Then
行是您倒置的CONTINUE。也就是说,如果doCol
保持为False,则脚本将继续到下一个单元格,并且不执行任何操作。
肯定不如正确的continue
或next for
语句那样快速/高效,但是最终结果与我所能达到的程度差不多。
答案 7 :(得分:0)
对于您不使用“ DO”的情况:这是我对带有嵌套If条件语句的FOR EACH的解决方案:
For Each line In lines
If <1st condition> Then
<code if 1st condition>
If <2nd condition> Then
If <3rd condition> Then
GoTo ContinueForEach
Else
<code else 3rd condition>
End If
Else
<code else 2nd condition>
End If
Else
<code else 1st condition>
End If
ContinueForEach:
Next
答案 8 :(得分:-1)
我有时会做一个双重循环:
Do
Do
If I_Don't_Want_to_Finish_This_Loop Then Exit Do
Exit Do
Loop
Loop Until Done
这避免了“goto spaghetti”