需要重新启动Excel VBA Do ...直到循环

时间:2015-07-08 21:01:16

标签: excel-vba vba excel

以下是我将寻求帮助的代码:

For i = 1 To numPlayers
    replacementDone = 0
    Do Until replacementDone <> 0
        replaceCards = InputBox("blah...blah")
        If replaceCards <> 0 Then
            replace100 = Application.Round((replaceCards / 100), 0)
            If replace100 < 5 Then
                Players(i, replace100) = Deck(DeckIndex)
                DeckIndex = DeckIndex + 1
            Else
                MsgBox "Invalid entry - try again."
            End If
            replace10 = Application.Round(Application.Mod(replaceCards, 100) / 10, 0)
            If replace10 < 5 And replace10 > replace100 Then
                Players(i, replace10) = Deck(DeckIndex)
                DeckIndex = DeckIndex + 1
            Else
                MsgBox "Invalid entry - try again."
            End If
            replace1 = Application.Round(Application.Mod(replaceCards, 10), 0)
            If replace1 < 5 And replace1 > replace10 Then
                Players(i, replace1) = Deck(DeckIndex)
                DeckIndex = DeckIndex + 1
            Else
                MsgBox "Invalid entry - try again."
            End If
        End If
    Loop
Next i

If语句不真实且要显示MsgBox "Invalid entry - try again."的每个点上,我想重新启动Do Until循环。我已经看到的关于重新启动Do Until循环的所有响应都解决了用户的代码逻辑,而不是确定如何重新启动。非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

一种方法是使用&#34; goto&#34;声明,如:

For i = 1 To numPlayers
    replacementDone = 0

    LoopStart:
    Do Until replacementDone <> 0
        replaceCards = InputBox("blah...blah")
        If replaceCards <> 0 Then
            replace100 = Application.Round((replaceCards / 100), 0)
            If replace100 < 5 Then
                Players(i, replace100) = Deck(DeckIndex)
                DeckIndex = DeckIndex + 1
            Else
                MsgBox "Invalid entry - try again."
                GoTo LoopStart
            End If
            replace10 = Application.Round(Application.Mod(replaceCards, 100) / 10, 0)
            If replace10 < 5 And replace10 > replace100 Then
                Players(i, replace10) = Deck(DeckIndex)
                DeckIndex = DeckIndex + 1
            Else
                MsgBox "Invalid entry - try again."
                GoTo LoopStart
            End If
            replace1 = Application.Round(Application.Mod(replaceCards, 10), 0)
            If replace1 < 5 And replace1 > replace10 Then
                Players(i, replace1) = Deck(DeckIndex)
                DeckIndex = DeckIndex + 1
            Else
                MsgBox "Invalid entry - try again."
                GoTo LoopStart
            End If
        End If
    Loop
Next i