接下来没有For Vba Compile错误

时间:2015-11-05 10:54:11

标签: excel vba excel-vba

我的代码无法编译,错误消息为

下一步没有

我该怎么办?

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 个答案:

答案 0 :(得分:1)

你也可以将它们全部保存在一行中,我相信你还有其他错误,这应该解决它们。

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

答案 1 :(得分:1)

你的代码中有很多错误,甚至会让那些回答的人感到困惑,所以就是这样:

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

此代码只读取第i行的值

编辑:我刚注意到:j = i,所以为什么要这么麻烦?

答案 2 :(得分:0)

您缺少几个End If语句。正确的代码应该是这样的:

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

答案 3 :(得分:0)

不是每次都开始一个新的If语句,最好使用ElseIf语句。然后你只需要使用一个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

答案 4 :(得分:0)

ElseIf
不同 Else
If
请正确学习:-)
或者使用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