VBA - how to conditionally skip a for loop iteration

后端 未结 6 1245
Happy的楠姐
Happy的楠姐 2020-11-30 01:23

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(Schedul         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 01:56

    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.

提交回复
热议问题