我的 VBA Excel 2013 代码未编译
My VBA Excel 2013 Code isn't compiling
当我运行这段代码时:
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
出现错误提示:
编译错误:Next without For
但是,每个 Next 肯定有一个 For。
那我哪里错了?
注意:'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
当我运行这段代码时:
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
出现错误提示:
编译错误:Next without For
但是,每个 Next 肯定有一个 For。
那我哪里错了?
注意:'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