What are some ways to simulate a Continue statement in VBA?
本问题已经有最佳答案,请猛点这里访问。
由于VBA没有Continue语句(或任何类似性质的语句),因此完成同一件事的另一种方法是什么。
在不提供Continue语句的语言中执行此操作的最佳方法是将其余代码块简单地包装为
1 2 3 4 5 6 | For i=1 to 10 'some code here If I_want_to_finish_this_loop 'do your thing End If Next i |
这样可以避免使用Goto,而您唯一的成本就是逆转条件。
如果您有多个需要继续的地方,最好的选择是在代码中放置一个继续标签,然后转到它。
1 2 3 4 5 6 7 8 | For i=1 to 10 'some code here If I_dont_want_to_finish_this_loop Goto Continue End If 'finish the loop :Continue Next i |
一种方法是使用封闭的Do ... Loop。 当然,这不能在另一个Do ... Loop中使用,但是我们在大多数编程语言中都遇到了这种情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | For i=1 To 10 Do 'Do everything in here and If I_Dont_Want_Finish_This_Loop Then Exit Do End If 'Of course, if I do want to finish it, 'I put more stuff here, and then... Loop While False 'quit after one loop Next i |