不编译因为" Next没有For"

时间:2018-06-01 01:01:22

标签: loops access-vba

我有以下代码,但是它(在最后For循环中)抱怨我Next没有For因此赢了&# 39;甚至编译所以我可以调试!

我猜我有一些方向问题,但如果它不能编译开始,我甚至无法调查它。

For Each ck In Screen.ActiveForm
    For Each oCol In OverlapCol
        If oCol = Right(ck.Name, Len(ck.Name) - 2) Then
        ck.Enabled = True
        Exit For
        GoTo TrueCk
        End If
    Next oCol
    For Each aCol In DistAutCol
        If aCol = Right(ck.Name, Len(ck.Name) - 2) Then
        ck.Enabled = True
        GoTo TrueCk
        Exit For
        End If
    Next aCol
    For Each gCol In DistGenCol
        If gCol = Right(ck.Name, Len(ck.Name) - 2) Then
        ck.Enabled = True
        Exit For
        GoTo TrueCk
    Next gCol
    ck.Enabled = False
TrueCk:
Next ck

我不明白:For循环到其他地方有什么不同?为什么我会遇到这个问题?

1 个答案:

答案 0 :(得分:1)

我建议在ifs中使用缩进格式化格式:

    For Each ck In Screen.ActiveForm
        For Each oCol In OverlapCol
            If oCol = Right(ck.Name, Len(ck.Name) - 2) Then
                ck.Enabled = True
                Exit For
                GoTo TrueCk
            End If
        Next oCol
        For Each aCol In DistAutCol
            If aCol = Right(ck.Name, Len(ck.Name) - 2) Then
                ck.Enabled = True
                GoTo TrueCk
                Exit For
            End If
        Next aCol
        For Each gCol In DistGenCol
            If gCol = Right(ck.Name, Len(ck.Name) - 2) Then
                ck.Enabled = True
                Exit For
                GoTo TrueCk
            End If
        Next gCol
        ck.Enabled = False
TrueCk:
    Next ck

在这种情况下,您会注意到,您忘记在最后End If中添加If,遗憾的是编译器消息在这种情况下会产生误导。