当我运行此代码时:
Private Sub Workbook_Open()
Dim i As Integer
Dim j As Integer
Dim range1 As Integer
Dim range2 As Integer
range1 = 53
range2 = 102
For i = range1 To range2
For j = (range1 - 50) To (range2 - 50)
If Cells(2, i) = Cells(2, j) Then
If Cells(7, i) > Cells(7, j) Then
Cells(2, i).Interior.ColorIndex = 37 'Went up; Green
ElseIf Cells(7, i) = Cells(7, j) Then
Cells(2, i).Interior.ColorIndex = 37 'No change; Grey
Else
Cells(2, i).Interior.ColorIndex = 37 'Went down; Red
End If
Next j
If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill
Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue
End If
Next i
End Sub
出现一个错误说:
编译错误:接下来没有
但是,每个下一个肯定有一个。
那我怎么了?
注意:" 37"只是填充号,我知道它看起来像浅蓝色。
以一致的方式缩进您的代码,给出以下内容:
Private Sub Workbook_Open()
Dim i As Integer
Dim j As Integer
Dim range1 As Integer
Dim range2 As Integer
range1 = 53
range2 = 102
For i = range1 To range2
For j = (range1 - 50) To (range2 - 50)
If Cells(2, i) = Cells(2, j) Then
If Cells(7, i) > Cells(7, j) Then
Cells(2, i).Interior.ColorIndex = 37 'Went up; Green
ElseIf Cells(7, i) = Cells(7, j) Then
Cells(2, i).Interior.ColorIndex = 37 'No change; Grey
Else
Cells(2, i).Interior.ColorIndex = 37 'Went down; Red
End If
Next j ' <--- This Next has no For associated with it
If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill
Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue
End If
Next i
End Sub
您可以通过缩进级别非常快速地分辨出Next j
在当前If
块中没有与之关联的For
语句。这就是为什么您会出错的原因。
我怀疑您打算在该Next j
之前拥有End If
,因此您的代码看起来像:
Private Sub Workbook_Open()
Dim i As Integer
Dim j As Integer
Dim range1 As Integer
Dim range2 As Integer
range1 = 53
range2 = 102
For i = range1 To range2
For j = (range1 - 50) To (range2 - 50)
If Cells(2, i) = Cells(2, j) Then
If Cells(7, i) > Cells(7, j) Then
Cells(2, i).Interior.ColorIndex = 37 'Went up; Green
ElseIf Cells(7, i) = Cells(7, j) Then
Cells(2, i).Interior.ColorIndex = 37 'No change; Grey
Else
Cells(2, i).Interior.ColorIndex = 37 'Went down; Red
End If
End If
Next j
If Cells(2, i).Interior.ColorIndex = 0 Then 'Hasn't Changed; No Fill
Cells(2, i).Interior.ColorIndex = 37 'New Song; Blue
End If
Next i
End Sub