下一步没有Vba编译错误

下一步没有Vba编译错误,vba,excel,Vba,Excel,我的代码没有编译,错误消息是 下一个没有 我能做什么 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

我的代码没有编译,错误消息是

下一个没有

我能做什么

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

您缺少几个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

不要每次都启动一个新的
If
语句,最好使用
ElseIf
语句。如果,则只需使用一个
End

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

ElseIf

Else

如果

请学会正确识别:-)
或使用


你也可以把它们都放在一行,我相信你还有其他错误,这应该可以解决它们

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

您的代码中存在许多错误,甚至使回答者感到困惑,因此,就是这样:

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一直都在,为什么要麻烦呢?

在下一个
之前,尝试
ElseIf
而不是
else
&
if
缺失
结束if
。您可以通过正确的代码列表来避免这种情况(
else
if
位于同一列)。而且,您将相同的值作为条件检查3倍,那么为什么不使用
选择案例
?谢谢大家,我选择了@iDevlop解决方案,但在运行代码时什么也没有发生,这是我第一次使用Vbaway@ulysse-pacomekoudou:我建议您学习1)创建断点2)以分步模式(F8)运行代码3)使用即时窗口在中断模式下检查值是的,很公平,我需要从某个地方开始,我从昨天开始对代码做了一些更改,但它仍然不运行。你在使用我的代码吗?它确实有效。也许在你的问题中描述一下你到底想做什么。不过看起来确实很明显。@DaveXcel你可能是对的。我把注意力集中在
下一个不带for的
方面,这就是问题所在。我在这一步可能错了,请随意更正我的代码谢谢@patrick我指的是第一列“A”(数据输入),j指的是“B”列,结果必须是(ouptup)
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