Isaac Reefman
Isaac Reefman

Reputation: 597

Not compiling because of "Next without For"

I have the following code, but it's complaining (on the last For loop) that I've got a Next without For and so won't even compile so I can debug!

I'm guessing I have some direction issues, but I can't even look into that if it won't compile to start with!

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

I don't get it: What's different about that For loop to the rest? Why am I getting this problem?

Upvotes: 0

Views: 31

Answers (1)

Sergey S.
Sergey S.

Reputation: 6336

I would recommend to make formatting with indents in Ifs like this:

    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

In this case you would notice, that you forgot to add End If in last If, unfortunately compiler message is misleading in such kind cases.

Upvotes: 1

Related Questions