Excel VBA - 如何在“直到……循环”中跳到下一次迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39867524/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:35:07  来源:igfitidea点击:

Excel VBA - how to skip to next iteration in a Do Until...Loop

vbaexcel-vbaexcel

提问by Tzvibe

I'm trying to skip to the next iteration in my vba code, with the 'continue' statement. Its not working..

我正在尝试使用“继续”语句跳到我的 vba 代码中的下一次迭代。它不工作..

  Do Until IsEmpty(xlSheet.Cells(row, 1))
       row = row + 1
       If xlSheet.Cells(row, 2) = "Epic" Then
          Continue
       End If
       xlSheet.Cells(row, 1).Value = 5
  Loop

Any idea?? Thanks..

任何的想法??谢谢..

回答by YowE3K

VBA does not have a Continuestatement. You can get around it by doing something like

VBA 没有Continue声明。你可以通过做类似的事情来解决它

Do Until IsEmpty(xlSheet.Cells(row + 1, 1))
   row = row + 1
   If xlSheet.Cells(row, 2) <> "Epic" Then
       xlSheet.Cells(row, 1).Value = 5
   End If
Loop

or

或者

Do Until IsEmpty(xlSheet.Cells(row + 1, 1))
    row = row + 1
    If xlSheet.Cells(row, 2) = "Epic" Then
        GoTo ContinueDo
    End If
    xlSheet.Cells(row, 1).Value = 5
ContinueDo:
Loop


Note: In both versions I changed IsEmpty(xlSheet.Cells(row, 1))to IsEmpty(xlSheet.Cells(row + 1, 1))or else you will end up with an infinite loop.

注意:在我更改的两个版本中IsEmpty(xlSheet.Cells(row, 1))IsEmpty(xlSheet.Cells(row + 1, 1))否则您最终会陷入无限循环。

回答by Rob Duikersloot

So this is another possibility that works for me. Just skip the unwanted lines...

所以这是另一种对我有用的可能性。只需跳过不需要的行...

Do Until IsEmpty(xlSheet.Cells(row, 1))
   row = row + 1
   Do while xlSheet.Cells(row, 2) = "Epic" 
      row = row + 1
   Loop
   xlSheet.Cells(row, 1).Value = 5
Loop