VBA:跳出for循环

标签 vba excel loops

我如何实现以下目标?

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

最佳答案

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

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:跳出for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808424/

相关文章:

c - 通过二维指针数组循环的段错误

java - 循环内循环 - 显示ArrayList

vba - 在 VBA 中以编程方式创建文档并为其分配数据

vba - 循环遍历文件夹的各个子文件夹以及这些子文件夹的每个文件时出现问题

excel - 评价直线的直线度

excel - 初始化变量时类型不匹配错误

vba - Excel VBA : Auto numbering

database - MS Access - 打开一个表单,从以前的表单中获取字段值

vba - Excel VBA 用于在 Word 中创建编号列表

javascript - 加载 "..."循环,每 3 秒更改一次文本