Skipping iterations in for loops

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
I've seen this before, but I can't remember how to do it.

For i = 1 to 3
'code
Next

How do I skip, for instance, iteration 2?

Edit: Using i as a variable in the code as well as a counter, which is why this matters.
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
It's weird that didn't occur to me.

What if I only wanted to skip a single iteration?

"For i = 1 to 5", where "4" is skipped.

Is that possible without "If i = 4 then goto label" with the "label:" at the Next statement?
 

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,764
It's weird that didn't occur to me.

What if I only wanted to skip a single iteration?

"For i = 1 to 5", where "4" is skipped.

Is that possible without "If i = 4 then goto label" with the "label:" at the Next statement?
Code:
For i = 1 to 5
If i <> 4 Then
'your code here
End If
Next i
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
Office Version
  1. 365
Platform
  1. Windows
You don't need the Goto.
Code:
For I = 1 To 10
   If I<>4 Then
      ' Do stuff
      Debug.Print I
 
   End If
Next I
If there's more than 1 value you want to skip then you could expand the If or perhaps use Select case.

Though, it might be worth considering some other type of loop.
 

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Thanks njmack.

Norie, this is pure curiosity not a problem I'm tackling anymore, but do you have any suggestions on the other kinds of loops?

I know Do/Loop, For/Next, using labels and goto, and calling external subs or functions for use in expressions.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,205
Messages
5,527,402
Members
409,760
Latest member
zeeshansyed

This Week's Hot Topics

Top