4 Examples as Solution to VBA Continue Statement

As we know, VBA has “Exit For”, “Exit Do” to come out of the loop entirely – which is achieved by the break statement (normally in other programming languages).

How to achieve Continue Statement Purpose in VBA?

As we know, VBA has “Exit For”, and “Exit Do” to come out of the loop entirely – which is achieved by the break statement (normally in other programming languages).

However, VBA has no Continue statement  – to omit an iteration in the loop rather completely exiting the loop.

So, how we can achieve this? The topic of our tutorial.

First Solution – Using an If statement in the For loop

Generally, the continue statement is placed inside the for loop (in any language where it is available).

We are also using VBA If statement and testing the current value.

See how we will omit the current iteration while the loop still goes on until the condition is False.

VBA omit an iteration code:

Output:

VBA-continue

You can see, 4 is not displayed in the MsgBox.

If we have not used the If..Else in the above example, the output should be:

VBA-simple-For

Omitting multiple iterations

Just add more items by using Or operator in the If Statement and you may omit more iterations. For example, we will omit 3, 5, and 7 values in our above example. See the code and output below:

Code:

Output:

VBA-continue-multiple

VBA “Continue” with Array example

Similarly, you may omit the iteration(s) as working with For..Each and arrays.

In the example below, we have five array elements. The array contains fruits’ names. Normally, it will display the array elements as below:

Code:

Output:

VBA-continue-array

To demonstrate “Continue”, we will omit two fruit names by using If..Else statements:

Code:

Output:

VBA-continue-omit-arra

You can see, the two fruit names are not displayed in the message box.