VBA - 如何有条件地跳过 for 循环迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8680640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA - how to conditionally skip a for loop iteration
提问by Richard H
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 循环。我想要做的是测试循环中的某个条件,如果为真则跳到下一次迭代:
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.
但我希望能够在 PrevCouponIndex 变量中记录 i 的最后一个值。
Any ideas?
有任何想法吗?
Thanks
谢谢
采纳答案by Brian
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
回答by mwolfe02
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:
VBA 没有 aContinue
或任何其他等效关键字来立即跳转到下一个循环迭代。我建议明智地使用 来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 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.
但是,如果这确实是您的全部代码,那么@Brian 是绝对正确的。只需Else
在您的If
语句中添加一个子句并完成它。
回答by Unhandled Exception
You can use a kind of continue
by using a nested Do ... Loop While False
:
您可以continue
通过使用嵌套来使用一种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
回答by Jon Egerton
Continue For
isn't valid in VBA or VB6.
Continue For
在 VBA 或 VB6 中无效。
From this MSDN pageit looks to have been introduced into VB.Net in VS 2005./Net 2.
从这个 MSDN 页面看来,它已经被引入到 VS 2005./Net 2 中的 VB.Net 中。
As the others have said there's not really an option other than to use Goto
or an Else
.
正如其他人所说,除了使用Goto
或Else
.
回答by Singaravelan
Hi I am also facing this issue and I solve this using below example code
嗨,我也面临这个问题,我使用下面的示例代码解决了这个问题
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
回答by richo7
Maybe try putting it all in the end if and use a else to skip the code this will make it so that you are able not use the GoTo.
也许尝试将其全部放在最后 if 并使用 else 跳过代码,这将使您无法使用 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