Compile Error: Next Without For

Searched but I think my code is correct now, but still get this dreaded compile error, but I cannot determine why! Code is as follows:

Dim LastRow As Long
Dim i As Integer

LastRow = Cells(1, "A").End(xlDown).Row

For i = 2 To LastRow

If Cells(i, 19).Offset(0, -6).Value = "4125551212" Then
    Cells(i, 19).Value = ""

    Else
        If Cells(i, 19).Offset(0, -6).Value = Cells(i, 19).Offset(1, -6).Value And Cells(i, 19).Offset(1, -6).Value = Cells(i, 19).Offset(0, -6).Value Then
        Cells(i, 19).Value = "Y"

    Else
        If Cells(i, 19).Offset(0, -6).Value = Cells(i, 19).Offset(1, -6).Value Then
        Cells(i, 19).Value = "Y"

    Else
        If Cells(i, 19).Offset(0, -6).Value = Cells(i, 19).Offset(-1, -6).Value And Cells(i, 19).Offset(-1, 0).Value = "Y" Then
        Cells(i, 19).Value = "Y"

    Else
        Cells(i, 19).Value = ""

End If

Next i
1

1 Answer

Fix the Else positions:

Sub jhgf()

    Dim LastRow As Long
    Dim i As Integer

    LastRow = Cells(1, "A").End(xlDown).Row

    For i = 2 To LastRow

        If Cells(i, 19).Offset(0, -6).Value = "4125551212" Then
            Cells(i, 19).Value = ""

            ElseIf Cells(i, 19).Offset(0, -6).Value = Cells(i, 19).Offset(1, -6).Value And Cells(i, 19).Offset(1, -6).Value = Cells(i, 19).Offset(0, -6).Value Then
                Cells(i, 19).Value = "Y"

            ElseIf Cells(i, 19).Offset(0, -6).Value = Cells(i, 19).Offset(1, -6).Value Then
                Cells(i, 19).Value = "Y"

            ElseIf Cells(i, 19).Offset(0, -6).Value = Cells(i, 19).Offset(-1, -6).Value And Cells(i, 19).Offset(-1, 0).Value = "Y" Then
                Cells(i, 19).Value = "Y"

            Else
                Cells(i, 19).Value = ""

        End If

    Next i
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest