Search code examples
vbaconditional-statementsloops

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


Solution

  • Couldn't you just do something simple like this?

    For i = LBound(Schedule, 1) To UBound(Schedule, 1)
      If (Schedule(i, 1) < ReferenceDate) Then
         PrevCouponIndex = i
      Else
         DF = Application.Run("SomeFunction"....)
         PV = PV + (DF * Coupon / CouponFrequency)
      End If
    Next