Vba 继续循环
我有以下代码Vba 继续循环,vba,loops,for-loop,next,Vba,Loops,For Loop,Next,我有以下代码 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 stuf
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
所以我想我可以简单地让语句然后下一个x
,但这会给出一个“no for statement declared”错误
那么,如果instr(sname,Configuration item),我可以在
后面加什么,然后
使它继续到x的下一个值呢?您正在考虑一个类似or的continue
语句,但是VBA没有这样的本机语句,您不能像那样使用VBA的next
您可以使用GoTo
语句来实现您想要做的事情,但是实际上,GoTo
应该保留在备选方案人为且不切实际的情况下
在您使用单个“继续”条件的情况下,有一个非常简单、清晰且可读的替代方案:
If Not InStr(sname, "Configuration item") Then
'// other code to copy paste 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
Do
'... 做你的循环将要做的事情
'必要时跳到循环的末尾:
如果然后转到ContinueLop
'... 如果不符合条件,则执行其他操作
ContinueLoop:
环
晚了几年,但这里有另一种选择
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
这样就避免了多年后的“去意大利面”。。。我喜欢这个:
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 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
rngAll.列中每个rngCol的
doCol=False'对于不使用“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
行中每行的
若然
<第一种情况下的代码>
若然
若然
继续前进
其他的
如果结束
其他的
如果结束
其他的
如果结束
继续延伸:
下一个
您可以通过一种简单的方法来实现,只需将for循环中使用的变量值更改为结束值,如示例所示
Sub TEST_ONLY()
对于i=1到10
ActiveSheet.Cells(i,1).Value=i
如果i=5,那么
i=10
如果结束
接下来我
End Sub
谢谢大家纠正我的拼写错误,我知道我的拼写很糟糕,我很感激大家能抽出时间来帮助我。为值得尊敬的goto语句的逻辑和理性选择欢呼+1这看起来是我见过的避免使用goto继续循环的最整洁的方法。我想你也可以采用同样的方法在其他情况下避免跳槽,就这点而言……答案不错。Alfredo Yong的答案是相同的,但Alfredo的答案紧凑,让我更容易阅读。哇。女士们,先生们,“做一次或退出继续做”的成语。这就像是一个后继动作,但打破了后继动作固有的达到不可预测状态的能力。-仍然会使代码阅读起来更加复杂,特别是如果你想做的事情很多的话在我看来,如果条件的逻辑反转变得太复杂而难以阅读或理解,这是很有用的。当你有几个条件时,比如在你的循环中,这是不那么清晰和可读的。随着代码嵌套得越来越深,编码人员需要更多的空间来读取代码。出于这个原因,GoTo在这里可能会更好,而阿伦·贝勒的答案是另一个不错的解决方案。我同意,对于另一个问题,这将是更好的答案。对于这个问题,我们似乎同意,对于那些寻求更通用的方法来解决VBA缺乏“继续”声明的人来说,下面的备选答案具有优势。我的意图只是通过权衡一般情况下的权衡来深化讨论。我遵循一条规则:“检查你的先决条件,如果它们失败,那么放弃”开始一个方法,或开始一个循环,同样的交易。如果可以的话,我会使用continue,但是我使用Goto,因为它会停止嵌套的级别。我不知道为什么这里的每个人都在关注“多个嵌套级别”,而问题显然只有一个?…嗯,这基本上是一样的,少了一些换行符…这几乎是一样的。是的,但是,如果问题不止一个continue
条件,则这是当前导致难看嵌套的问题的标准选择
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
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