vendredi 21 juin 2019

VBA if Else within For Next

I want to run a For Next loop. Within the loop I want to test for a condition. If the condition is met, I want to go to the next For loop. If the condition is not met, I want to do something else, then go to the next for loop.

See below.

    Sub TestForIfElse()

        Dim Counter As Integer
        Dim Counter2 as Integer
        Dim i As Integer

        For i = 1 To 5

            Counter = i

            If Counter > 3 Then  'GOTO NEXT i LOOP

        Next i   'ERROR MSG NEXT WITHOUT FOR

            Else

            Counter2 = Counter2 + i

            End If

        Next i   'GOTO NEXT i LOOP

    End Sub

Expected: Run For Next loop.
Within the loop test for condition.
If the condition is met, go to the next For loop. If the condition is not met, do something else, then go to the next for loop. Error message as marked on code block.

Aucun commentaire:

Enregistrer un commentaire