【问题标题】:VBA: jumping out of a for loopVBA:跳出for循环
【发布时间】:2010-05-11 06:11:16
【问题描述】:

如何实现以下目标?

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

【问题讨论】:

    标签: vba excel loops


    【解决方案1】:

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

    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
    

    【讨论】:

    • 不用担心,很高兴我能帮上忙。
    • 这确实是最好的方法。一些注意事项,在 VBA 中,没有 'Next i' 它只是 Next
    • 啊,谢谢戴夫。我从来没有做过任何 VBA,所以我不得不即兴创作。 ;) 我编辑了代码以匹配您的评论。
    • 没问题,VBA 是弱类型的怪物,它可能已经接受了它:S
    【解决方案2】:

    然后回答问题的另一半:

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

    【讨论】:

    • 这不符合我的要求,因为它完全退出了 for 循环。
    • 您的问题询问如何“跳出 for 循环”。从您的代码中,您似乎想要的实际上是“继续”语句,正如@Eric 所回答的那样,为了完整起见,我添加了“退出”。正如另一位回答者所提到的,使用 Else 语句也很好。给猫剥皮的方法有很多。
    【解决方案3】:

    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

    【讨论】:

    • 我认为当您在 IDE 中时关键字会自动大写,但如果您只是编写 VBScript 文件,它会忽略大小写。
    • VBA IDE 窗口无法识别您的建议。
    • 德拉特。我假设语法是一样的。
    【解决方案4】:

    这是一个太老的问题,但只是为了此页面的正确性和完整性,这是可以使用 Goto 而不会导致 Spaghetti 代码的情况之一...

    它将 VBA 的 Loop 用于几乎任何其他较新的编程语言,并为 VBA 程序员提供 Eric 在另一个答案中描述的相同功能 - 但 Eric 的答案在 VBA 中不起作用

        For n = 1 To something
            If needToExitLoopEntirely Then Exit For
            If condition Then GoTo ContinueFor        
            ' more code        
            ' ...           
            ' more code
    ContinueFor: '<=this is a label; 
        Next n
    

    【讨论】:

    • 其实我在这里回答是因为我尝试了Eric的明显合理的答案,结果发现它在VBA中不起作用。因此,为了本页的正确性和完整性,我注册了在 VBA 中继续 For 循环的唯一正确且有效的方法。
    • 尊敬的 Downvoter 先生:另外两个答案(Tom 和 Eric)没有达到 OP 的要求。我的独特之处在于实现了这一点,即回答 我如何实现以下目标?。当然还有其他方式来控制流量,但这种方式是完全可以接受的。如果您能告诉我为什么不赞成这个答案,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多