简体   繁体   中英

Need to Restart an Excel VBA Do…Until Loop

Here's the code about which I will seek assistance:

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

At each point where an If statement is untrue and the MsgBox "Invalid entry - try again." is to appear, I want to restart the Do Until loop. All responses I've seen regarding restarting a Do Until loop address the user's code logic rather than identifying how to do the restart. Help is greatly appreciated.

One way would be to use a "goto" statement, like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM