Next without For Vba Compile error

2019-08-10 07:51发布

There is my code that doesn't compile, the error message is

Next Without For

What can I do ?

Sub CommandButton1_Click()

Dim i As Integer
Dim j As Integer
N = Range(Rows.Count, "A2").End(xlUp).Select

M = Range("B2").End(xlUp).Select

  For i = 1 To N

    If Cells(i, "A").Value = "2015 Xor 2011" Then
        Cells(j, "B").Value = "blue"

Else

    If Cells(i, "A").Value = "2001 Xor 2003" Then
        Cells(j, "B").Value = "green"


Else

    If Cells(i, "A").Value = "2014 Xor 2006" Then
        Cells(j, "B").Value = "red"

        j = j + 1

   End If
Next
End Sub

5条回答
迷人小祖宗
2楼-- · 2019-08-10 07:53

ElseIf is not the same as
Else
If
And please learn to ident properly :-)
Or use SmartIndenter.

Sub CommandButton1_Click()
    Dim i As Integer
    Dim j As Integer
    N = Range(Rows.Count, "A2").End(xlUp).Select

    M = Range("B2").End(xlUp).Select

    For i = 1 To N

        If Cells(i, "A").Value = "2015 Xor 2011" Then
            Cells(j, "B").Value = "blue"

        ElseIf Cells(i, "A").Value = "2001 Xor 2003" Then
            Cells(j, "B").Value = "green"

        ElseIf Cells(i, "A").Value = "2014 Xor 2006" Then
            Cells(j, "B").Value = "red"
            j = j + 1

        End If
    Next
End Sub
查看更多
The star\"
3楼-- · 2019-08-10 07:54

You are missing a couple of End If statements. The correct code should be like this:

Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer

    N = Range(Rows.Count, "A2").End(xlUp).Select

    M = Range("B2").End(xlUp).Select

    For i = 1 To N

        If Cells(i, "A").Value = "2015 Xor 2011" Then
            Cells(j, "B").Value = "blue"
        Else

            If Cells(i, "A").Value = "2001 Xor 2003" Then
                Cells(j, "B").Value = "green"
            Else

                If Cells(i, "A").Value = "2014 Xor 2006" Then
                    Cells(j, "B").Value = "red"

                    j = j + 1
                End If
            End If
        End If
    Next
End Sub
查看更多
戒情不戒烟
4楼-- · 2019-08-10 07:57

You can keep them all in one line as well, also I believe you have other errors, this should fix them up.

Sub Button1_Click()
    Dim i As Integer
    Dim N As Long
    'M As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    'M = cells(Rows.Count, "B").End(xlUp).Row

    For i = 1 To N

        If Cells(i, "A").Value = "2015 Xor 2011" Then Cells(i, "A").Offset(, 1).Value = "blue"
        If Cells(i, "A").Value = "2001 Xor 2003" Then Cells(i, "A").Offset(, 1).Value = "green"
        If Cells(i, "A").Value = "2014 Xor 2006" Then Cells(i, "A").Offset(, 1).Value = "red"

    Next

End Sub
查看更多
Explosion°爆炸
5楼-- · 2019-08-10 08:11

many mistackes in your code, even confusing those who answered, so this is it:

Option Explicit 'this avoids forgetting declaring variable, i put it on top of each code

Sub CommandButton1_Click()

Dim i&, j&, n& 'as Long, not integer

'declare and assign sheets:
Dim Ws As Worksheet
Set Ws = ActiveSheet ' you might want to correctely name the sheet, exept if the sub has to be dynamic with anysheet where you are...

'you missed declaring N !! so like you wrote it it looks like a range and 'to n' will mean to N.value
n = Ws.Range(Ws.Rows.Count, "A").End(xlUp).Row ' garbage => .Select
'supposing n is supposed to give the last line with something inside in column ("A"=1)

'garbage and not declared (and why select, again!?) => M = Range("B2").End(xlUp).Select
'j=1 'if ommited, on first loop it tries to write at row 0 (=>errpr) 
With Ws
      For i = 1 To n
           Select Case .Cells(i, "A").Value2 'like this the code is 3 times faster (and with arrays even faster , but no need to complicate...)                                                                      'note i use .value2 and not .value, faster but not working with dates or time formating of cells.
                Case "2015 Xor 2011":   .Cells(i, 2).Value2 = "blue"
                Case "2001 Xor 2003":   .Cells(i, 2).Value2 = "green"
                Case "2014 Xor 2006":   .Cells(i, 2).Value2 = "red"
           End Select
           'j = j + 1
      Next i
End With
End Sub

this code reads only once the value at the row i

edit : i just noticed: j=i at all times so why bother ?

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-08-10 08:15

Instead of starting a new If statement each time, you would be best off using the ElseIf statement. Then you only have to use one End If.

For i = 1 To N
    If Cells(i, "A").Value = "2015 Xor 2011" Then
        Cells(j, "B").Value = "blue"
    ElseIf Cells(i, "A").Value = "2001 Xor 2003" Then
        Cells(j, "B").Value = "green"
    ElseIf Cells(i, "A").Value = "2014 Xor 2006" Then
        Cells(j, "B").Value = "red"
        j = j + 1
    End If
Next i
查看更多
登录 后发表回答