如何退出/跳过外部尽管完成后如果 - 然后在嵌套(内部)做 - 而

问题描述:

我是VBA的新手处理一些实际工作问题,我有一个问题退出做 - 而完成后内部(嵌套)do-while。如何退出/跳过外部尽管完成后如果 - 然后在嵌套(内部)做 - 而

*代码工作正常,没有问题,但我想知道如何通过退出/跳过外部操作来提高效率 - 一旦内部if-then匹配并且完成##。

这里是代码的样子(FYI我删除一些代码为了便于阅读):

For Each w In Worksheets 
    If() Then 

     Do While (i) ' outter do-while 

      Do While() 'inner(nested/1st do-while) 
        If() Then 'match something here 

         ## do something here 

         Exit Do  'break the inner do-while 
        End If 

      Loop 

      i = i + 20 'question here!!how should I break outter do-while? 

     Loop      
    End If 
    Next w 

所以,我的目标是,一旦IF-THEN匹配和##的内容完成后,如何我可以直接去“下一个W”,又跳到下一个工作表?

我试过“goto + line”,但它不起作用,有没有另一种方法?

+0

加上'Boolean'类型的附加'variable'。在内部循环if语句中将它设置为'true'。接下来,在外层循环中检查这个'variable = true',如果是这样,则调用'exit do'作为外层循环。 –

+0

谢谢你Kazi!这非常有帮助! – leveygao

,你可以尝试这样的事:

Dim exitLoop As Boolean '<--| default initial value is 'False' 

For Each w In Worksheets 
    If() Then 

     Do While (i) And Not exitLoop ' outer do-while 

      Do While() and Not exitLoop 'inner(nested/1st do-while) 
        If() Then 'match something here 

         ## do something here 

         exitLoop = True 
         Exit Do  'break the inner do-while 
        End If 

      Loop 

      i = i + 20 'question here!!how should I break outter do-while? 

     Loop 
     exitLoop = False '<--| set back default value 
    End If 
Next w