Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA - how to conditionally skip a for loop iteration

I have a for loop over an array. What I want to do is test for a certain condition in the loop and skip to the next iteration if true:

For i = LBound(Schedule, 1) To UBound(Schedule, 1)     If (Schedule(i, 1) < ReferenceDate) Then         PrevCouponIndex = i         Continue   '*** THIS LINE DOESN'T COMPILE, nor does "Next"     End If     DF = Application.Run("SomeFunction"....)     PV = PV + (DF * Coupon / CouponFrequency) Next 

I Know I can do:

 If (Schedule(i, 1) < ReferenceDate) Then Continue For 

but I want to be able to record the last value of i in the PrevCouponIndex variable.

Any ideas?

Thanks

like image 308
Richard H Avatar asked Dec 30 '11 14:12

Richard H


People also ask

How do I skip iteration for a loop?

The continue statement (with or without a label reference) can only be used to skip one loop iteration. The break statement, without a label reference, can only be used to jump out of a loop or a switch.

How do you break a for loop in Excel VBA?

A Exit For statement is used when we want to exit the For Loop based on certain criteria. When Exit For is executed, the control jumps to the next statement immediately after the For Loop.

How do you skip loop iteration in VB?

If you have nested loops of different types, for example a Do loop within a For loop, you can skip to the next iteration of either loop by using either Continue Do or Continue For .

How do I skip in VBA?

The number of elements to skip is identified by the count parameter. You can use the Skip clause with the Take clause to return a range of data from any segment of a query. To do this, pass the index of the first element of the range to the Skip clause and the size of the range to the Take clause.


2 Answers

VBA does not have a Continue or any other equivalent keyword to immediately jump to the next loop iteration. I would suggest a judicious use of Goto as a workaround, especially if this is just a contrived example and your real code is more complicated:

For i = LBound(Schedule, 1) To UBound(Schedule, 1)     If (Schedule(i, 1) < ReferenceDate) Then         PrevCouponIndex = i         Goto NextIteration     End If     DF = Application.Run("SomeFunction"....)     PV = PV + (DF * Coupon / CouponFrequency)     '....'     'a whole bunch of other code you are not showing us'     '....'     NextIteration: Next 

If that is really all of your code, though, @Brian is absolutely correct. Just put an Else clause in your If statement and be done with it.

like image 110
mwolfe02 Avatar answered Sep 28 '22 23:09

mwolfe02


You can use a kind of continue by using a nested Do ... Loop While False:

'This sample will output 1 and 3 only  Dim i As Integer  For i = 1 To 3: Do      If i = 2 Then Exit Do 'Exit Do is the Continue      Debug.Print i  Loop While False: Next i 
like image 27
Unhandled Exception Avatar answered Sep 28 '22 22:09

Unhandled Exception