以一致的方式缩进代码可以得到以下结果:
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
没有
For
在当前
If
块这就是为什么你会出错。
我怀疑你打算
End If
就在那之前
下一个j
因此,您的代码如下所示:
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