2011-12-30 361 views
72

我有一个数组循环。我想要做的就是测试在循环一定条件并跳到下一个迭代,如果真:我知道我可以做VBA - 如何有条件地跳过for循环迭代

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变量中记录我的最后一个值。

任何想法?

谢谢

+1

你说:“我知道我可以做的:'如果(附表(I,1) mwolfe02 2011-12-30 14:59:32

+0

@ mwolfe02 - 不知道,但在例子的地方看到了(cpearson?) – 2011-12-30 16:21:11

+0

可能是一个VB.NET的例子 – 2014-08-08 03:49:05

回答

26

难道你不能只是做一些简单的事情吗?

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 
+2

确实,正是我所做的:)但仍然错误我,我必须在东西包装的东西片。谢谢 – 2011-12-30 16:22:12

+3

+1 @RichardH好吧,你必须使用一个'IF'来进行测试,所以这个代码并不昂贵。您应该确保最常见的结果是'Schedule(i,1)'小于'ReferenceDate',以避免经常执行Else。否则使用'(ReferenceDate> = Schedule(i,1))'。 (如果测试是50/50,那么不需要优化) – brettdj 2011-12-31 01:17:17

140

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 

如果这真的是所有的代码,但是,@布赖恩是绝对正确。只需在您的If声明中添加Else条款并完成它。

+15

谢谢,这是一个很好的提示转到GoTo(VBA - 回到1964年) – 2011-12-30 16:23:21

+1

GoTo是EVIL!不要使用GoTo,否则你的代码会变得很糟糕!尽管如此,请不要在代码中使用GoTo。 – George 2015-02-24 18:08:41

+1

@George:GoTo可能会被滥用(这就是为什么我限定了我的陈述;请参阅[谨慎](http://dictionary.reference.com/browse/judicious)),但它本质上并非邪恶。但是,严重的是,仅仅因为你需要它进行错误处理(即On Error Goto'),没有Goto语句编写健壮的VBA是不可能的。 – mwolfe02 2015-02-25 00:55:13

11

Continue For在VBA或VB6中无效。

this MSDN page它看起来像别人说有没有真的比使用GotoElse以外的选项已被引入到VB.Net在VS 2005./Net 2

2

嗨,我也面临这样的问题,我该用下面的示例代码

For j = 1 To MyTemplte.Sheets.Count 

     If MyTemplte.Sheets(j).Visible = 0 Then 
      GoTo DoNothing   
     End If 


'process for this for loop 
DoNothing: 

Next j 
+0

不知道为什么这是倒票,下一个答案有100多票,他们是相同的答案! – rryanp 2016-10-12 14:02:09

+0

可能因为这个答案是在答案后5年写的,而且是完全相同的概念。为什么要收到upvotes? – 2017-04-13 19:25:31

-2

也许尝试把它所有的如果最终并使用其他跳过代码解决,这将让这个你不能使用GoTo。

     If 6 - ((Int_height(Int_Column - 1) - 1) + Int_direction(e, 1)) = 7 Or (Int_Column - 1) + Int_direction(e, 0) = -1 Or (Int_Column - 1) + Int_direction(e, 0) = 7 Then 
       Else 
        If Grid((Int_Column - 1) + Int_direction(e, 0), 6 - ((Int_height(Int_Column - 1) - 1) + Int_direction(e, 1))) = "_" Then 
         Console.ReadLine() 
        End If 
       End If 
0

您可以通过使用嵌套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 i