VBA:跳出for循环

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

VBA: jumping out of a for loop

vbaexcel-vbaloopsexcel

提问by stanigator

How do I achieve the following?

我如何实现以下目标?

Sub Macro1()
'
' Macro1 Macro
'

'
    Worksheets("Drop-down").Select
    n = Cells(1, 1).End(xlDown).Row
    For i = 1 To n
        ActiveSheet.Cells(i, 2).Select
        *******************************************************
        If Worksheets("Misc").Cells(2, i).Value = "" Then
            continue i
        End If
        *******************************************************
        If Worksheets("Misc").Cells(3, i).Value <> "" Then
            Set validationRange = Range(Worksheets("Misc").Cells(2, i), Worksheets("Misc").Cells(2, i).End(xlDown))
        Else
            Set validationRange = Worksheets("Misc").Cells(2, i)
        End If
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=validationRange.Address
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
    Next i
End Sub

回答by Mia Clarke

Isn't this a simple flow control case? I don't understand the need for special keywords to solve it:

这不是一个简单的流量控制案例吗?我不明白需要特殊关键字来解决它:

For i = 0 To 10
 If Not condition Then 
    some other code
Next   

EDIT:Or, in your code:

编辑:或者,在您的代码中:

Sub Macro1()
'
' Macro1 Macro
'

'
    Worksheets("Drop-down").Select
    n = Cells(1, 1).End(xlDown).Row
    For i = 1 To n
        ActiveSheet.Cells(i, 2).Select
        *******************************************************
        If Not Worksheets("Misc").Cells(2, i).Value = "" Then
        *******************************************************
          If Worksheets("Misc").Cells(3, i).Value <> "" Then
              Set validationRange = Range(Worksheets("Misc").Cells(2, i), Worksheets("Misc").Cells(2, i).End(xlDown))
          Else
              Set validationRange = Worksheets("Misc").Cells(2, i)
          End If
          With Selection.Validation
              .Delete
              .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
              xlBetween, Formula1:=validationRange.Address
              .IgnoreBlank = True
              .InCellDropdown = True
              .InputTitle = ""
              .ErrorTitle = ""
              .InputMessage = ""
              .ErrorMessage = ""
              .ShowInput = True
              .ShowError = True
          End With
     ********
      End If
     ********
    Next 
End Sub

回答by Tom

And to answer the other half of the question:

并回答问题的另一半:

For n = 1 To something
    If condition Then
        Exit For
    End If
    ' more code

Next n

回答by Eric

Aren't vb's keywords capitalized?

vb的关键字不是大写吗?

For n = 1 To something
    If condition Then
        Continue For
    End If

    ' more code

Next n

The Continuekeyword does what you want.

Continue关键字你想要做什么。

http://msdn.microsoft.com/en-us/library/801hyx6f(VS.80).aspx

http://msdn.microsoft.com/en-us/library/801hyx6f(VS.80).aspx