vba 继续 For 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Continue For loop
提问by DevilWAH
I have the following code
我有以下代码
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
So I thought I could simply have the statement Then Next x
, but this gives a "no for statement declared" error.
所以我想我可以简单地使用 statement Then Next x
,但这给出了“没有声明声明”错误。
So what can I put after the If instr(sname, "Configuration item") Then
to make it proceed to the next value for x?
那么我可以在 之后放什么If instr(sname, "Configuration item") Then
来使它进入 x 的下一个值?
采纳答案by Jean-Fran?ois Corbett
You're thinking of a continue
statement like Java'sor Python's, but VBA has no such native statement, and you can't use VBA's Next
like that.
您正在考虑Java或Python 之continue
类的语句,但 VBA 没有这样的本机语句,您不能像那样使用 VBA 。Next
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.
您可以使用GoTo
语句来实现类似您想要做的事情,但实际上,GoTo
应该保留用于替代方案是人为的和不切实际的情况。
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
回答by VBA hack
You can use a GoTo
:
您可以使用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
回答by Arlen Beiler
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
回答by Alfredo Yong
A lot of years after... I like this one:
很多年后......我喜欢这个:
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
回答by moongster
A few years late, but here is another alternative.
晚了几年,但这是另一种选择。
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
回答by cssyphus
This can also be solved using a boolean.
这也可以使用布尔值来解决。
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
For example, here is the full example that:
(1) Identifies range of used cells on worksheet
(2) Loops through each column
(3) IF column title is an accepted title, Loops through all cells in the column
例如,以下是完整示例:
(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
Note: If you didn't immediately catch it, the line If docol Then
is your inverted CONTINUE. That is, if doCol
remains False, the script CONTINUES to the next cell and doesn't do anything.
注意:如果你没有立即抓住它,这条线If docol Then
就是你的倒置 CONTINUE。也就是说,如果doCol
仍然为 False,则脚本继续到下一个单元格并且不执行任何操作。
Certainly not as fast/efficient as a proper continue
or next for
statement, but the end result is as close as I've been able to get.
当然不如正确continue
或next for
声明那么快/有效,但最终结果与我所能得到的一样接近。
回答by Rand
I sometimes do a double do loop:
我有时会做一个双循环:
Do
Do
If I_Don't_Want_to_Finish_This_Loop Then Exit Do
Exit Do
Loop
Loop Until Done
This avoids having "goto spaghetti"
这避免了“转到意大利面”