过早推进循环的方法

时间:2016-09-16 16:14:48

标签: excel vba excel-vba

除了使用GOTO之外,是否有办法在循环中放弃一大块代码以将其推进到下一个? GOTO在下面的代码中是一种有效的方法吗?

Do While x < 10

    a = Int(Rnd * 100)
    b = Int(Rnd * 100)
    c = Int(Rnd * 100)

    For n = 1 To 4
        aa(i) = i * 2 + 4
        bb(i) = i * 2 + 5
        cc(i) = i * 2 + 6
    Next

    If a = b Then GoTo lastline
    If a = c Then GoTo lastline
    If b = c Then GoTo lastline

    For i = 1 To 4

        testa = (a = aa(i) Or a = bb(i) Or a = cc(i))
        If testa Then GoTo lastline

        testb = (b = aa(i) Or b = bb(i) Or b = cc(i))
        If testb Then GoTo lastline

        testc = (c = aa(i) Or c = bb(i) Or c = cc(i))
        If testc Then GoTo lastline
    Next

    Debug.Print a & ","; b & ","; c

lastline:
    x = x + 1

Loop

4 个答案:

答案 0 :(得分:2)

我只想用if语句包装可能跳过的部分。 第二个For循环也可以在Exit For中提前退出。这样做可以避免使用GoTo。我还没有测试下面的代码,但它应该说明这个概念。

Do While x < 10
    a = Int(Rnd * 100)
    b = Int(Rnd * 100)
    c = Int(Rnd * 100)
    For n = 1 To 4
        aa(i) = i * 2 + 4
        bb(i) = i * 2 + 5
        cc(i) = i * 2 + 6
    Next
    If Not a = b Or Not a = c Or Not b = c Then
        For i = 1 To 4
            testa = (a = aa(i) Or a = bb(i) Or a = cc(i))
            testb = (b = aa(i) Or b = bb(i) Or b = cc(i))
            testc = (c = aa(i) Or c = bb(i) Or c = cc(i))
            If testa Or testb Or testC Then
                Exit For
            Else
                If i = 4 Then
                    Debug.Print a & ","; b & ","; c
                End If
            End If
        Next
    End If
    x = x + 1
Loop

答案 1 :(得分:1)

为了完整起见,如果您提取Sub来完成工作,这就是它的样子。请注意,我 假设 是原始代码中的拼写错误:

For n = 1 To 4
    aa(i) = i * 2 + 4    '<--- 'i'?  shouldn't these be 'n'?
    bb(i) = i * 2 + 5
    cc(i) = i * 2 + 6
Next

由于您稍后使用i作为循环计数器,因此总是是第一次进入循环的任何内容,之后每次都是4。我的猜测是,这不是你想要的。

完全没有...提取Sub

Private Sub WhateverYoureDoing(aa As Variant, bb As Variant, cc As Variant)
    Dim a As Long, b As Long, c As Long

    a = Int(Rnd * 100)
    b = Int(Rnd * 100)
    c = Int(Rnd * 100)

    'Assuming this should be 'i' instead of 'n' as the loop count.
    For i = 1 To 4
        aa(i) = i * 2 + 4
        bb(i) = i * 2 + 5
        cc(i) = i * 2 + 6
    Next

    If a = b Then Exit Sub
    If a = c Then Exit Sub
    If b = c Then Exit Sub

    For i = 1 To 4
        If a = aa(i) Or a = bb(i) Or a = cc(i) Then Exit Sub
        If b = aa(i) Or b = bb(i) Or b = cc(i) Then Exit Sub
        If c = aa(i) Or c = bb(i) Or c = cc(i) Then Exit Sub
    Next

    Debug.Print a & ","; b & ","; c
End Sub

致电代码:

Do While x < 10
    WhateverYoureDoing aa, bb, cc
    x = x + 1
Loop

请注意,您可以极大地简化此代码。例如,这......

a = Int(Rnd * 100)
b = Int(Rnd * 100)
c = Int(Rnd * 100)
'...
If a = b Then GoTo lastline
If a = c Then GoTo lastline
If b = c Then GoTo lastline

......可以简单地说:

Do
    a = Int(Rnd * 100)
    b = Int(Rnd * 100)
    c = Int(Rnd * 100)
Loop While a <> b And a <> c And b <> c

答案 2 :(得分:0)

如前所述,你会在错误时使用IF跳过:

Do While x < 10

    a = Int(Rnd * 100)
    b = Int(Rnd * 100)
    c = Int(Rnd * 100)

    For n = 1 To 4
        aa(i) = i * 2 + 4
        bb(i) = i * 2 + 5
        cc(i) = i * 2 + 6
    Next

    If Not a = b Or Not a = c Or Not b = c Then

        For i = 1 To 4

            testa = (a = aa(i) Or a = bb(i) Or a = cc(i))
            If Not testa Then
                testb = (b = aa(i) Or b = bb(i) Or b = cc(i))
                If Not testb Then
                    testc = (c = aa(i) Or c = bb(i) Or c = cc(i))
                    If Not testc Then
                        Debug.Print a & ","; b & ","; c
                    End If
                End If
            End If
        Next

    End If

    x = x + 1

Loop

答案 3 :(得分:0)

您可以通过多种不同方式使用Exit,包括Exit For,以摆脱For...Next循环。

Here is the Exit MSDN

  

退出

     

立即退出出现的For循环。继续执行Next语句后面的语句。 Exit For只能在For ... Next或For Each ... Next循环中使用。在嵌套的For循环中使用时,Exit For退出最内层循环并将控制转移到下一个更高级别的嵌套。