接下来没有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 

你可以把它们全部放在一行,也相信你有其他的错误,这应该解决它们。

 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 =我在任何时候,为什么要麻烦?

你错过了一些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 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 

否则如果不一样
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