关于vba:继续循环

Continue For loop

我有以下代码

1
2
3
4
5
6
7
8
9
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的下一个值?


您可以使用GoTo

1
2
3
4
5
6
7
8
9
10
11
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


您正在考虑像Java或Python这样的continue语句,但是VBA没有这样的本机语句,并且您不能像这样使用VBA的Next

您可以使用GoTo语句来实现与您尝试执行的操作类似的操作,但实际上,GoTo应该保留给备选方案是人为的且不切实际的情况。

对于只有一个"连续"条件的情况,有一个非常简单,干净且可读的替代方法:

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


很多年后...我喜欢这个:

1
2
3
4
5
6
7
8
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


1
2
3
4
5
6
7
8
9
10
11
12
13
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


迟了几年,但这是另一种选择。

1
2
3
4
5
6
7
8
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


这也可以使用布尔值解决。

1
2
3
4
5
6
7
8
9
10
11
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)如果列标题是可接受的标题,则遍历列中的所有单元格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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,脚本将继续到下一个单元格,并且不执行任何操作。

当然,速度/效率不如正确的continuenext for语句快,但最终结果与我所能获得的接近。


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

1
2
3
4
5
6
7
8
9
10
11
Do

    Do

        If I_Don't_Want_to_Finish_This_Loop Then Exit Do

        Exit Do

    Loop

Loop Until Done

这样可以避免吃"意大利通心粉"