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.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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?
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,544
Members
452,925
Latest member
duyvmex

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top