VBA:跳出for循环

时间:2010-05-11 06:11:17

标签: vba excel-vba loops excel

如何实现以下目标?

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

4 个答案:

答案 0 :(得分:4)

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

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

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

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

答案 1 :(得分:2)

回答问题的另一半:

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

Next n

答案 2 :(得分:0)

vb的关键字不是大写的吗?

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

    ' more code

Next n

Continue关键字可以满足您的需求。

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

答案 3 :(得分:-1)

这是一个太旧的问题,但是仅出于此页面的正确性和完整性的考虑,这是可以在不产生意大利面条代码的情况下使用Goto的情况之一。

它将VBA的Loop应用于几乎所有其他较新的编程语言,并为VBA程序员提供了Eric在另一个答案中描述的相同功能:

    For n = 1 To something
        If condition Then GoTo ContinueFor        
        ' more code        
        ' ...
        ' more code
ContinueFor: '<=this is a label; 
    Next n
相关问题