我在数组上有一个for循环。我想要做的是测试循环中的某个条件,如果为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我知道我能做到:
If (Schedule(i, 1) < ReferenceDate) Then Continue For但是我希望能够在PrevCouponIndex变量中记录i的最后一个值。
有什么想法吗?
谢谢
发布于 2011-12-30 23:02:51
你就不能像这样做点简单的事情吗?
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发布于 2011-12-30 23:05:52
VBA没有Continue或任何其他等效的关键字来立即跳转到下一个循环迭代。我建议明智地使用Goto作为解决方法,特别是如果这只是一个人为的示例,并且您的真实代码更加复杂:
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如果这真的是你所有的代码,那么,@Brian是绝对正确的。只需在If语句中添加一个Else子句,然后就可以使用它了。
发布于 2018-02-26 16:21:06
您可以通过使用嵌套的Do ... Loop While False来使用一种continue
'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 ihttps://stackoverflow.com/questions/8680640
复制相似问题