我是新手在excel vba中编写代码而我收到编译错误Next Without For .....然而我有一个for和a next,请你告诉我我做错了什么,请看我的下面的代码,谢谢!
Dim i As Integer
For i = 19 To 49
If Cells(3, i) > 499999 Then
Cells(4, 3) = 499999
ElseIf Cells(3, i) < 499999 Then
Cells(4, 3) = Cells(3, i)
End If
If Cells(4, 3) < 499999 Then
Cells(4, 5) = 0
If Cells(3, i) > 999999 Then
Cells(4, 4) = 500000
ElseIf Cells(3, i) < 999999 Then
Cells(4, 4) = Cells(3, i) - 499999
End If
If Cells(4, 4) = 0 Then
Cells(4, 5) = 0
ElseIf Cells(3, i) > 1999999 Then
Cells(4, 5) = 1000000
ElseIf Cells(3, i) < 1999999 Then
Cells(4, 5) = Cells(3, i) - 999999
End If
If Cells(4, 5) = 0 Then
Cells(4, 6) = 0
ElseIf Cells(3, i) > 4999999 Then
Cells(4, 6) = 3000000
ElseIf Cells(3, i) < 4999999 Then
Cells(4, 6) = Cells(3, i) - 1999999
End If
If Cells(4, 6) = 3000000 Then
Cells(4, 7) = Cells(3, i) - 4999999
ElseIf Cells(4, 6) < 3000000 Then
Cells(4, 7) = 0
End If
Range(7, i).Value = (Cells(5, 3) + Cells(5, 4) + Cells(5, 5) + Cells(5, 6) + Cells(5, 7)) / Cells(3, i)
Next i
End Sub
答案 0 :(得分:5)
在第二个If
块中,替换
If Cells(4, 3) < 499999 Then
Cells(4, 5) = 0
If Cells(3, i) > 999999 Then
与
If Cells(4, 3) < 499999 Then
Cells(4, 5) = 0
ElseIf Cells(3, i) > 999999 Then
这使VBA解释器感到困惑。
答案 1 :(得分:2)
If Cells(4, 3) < 499999 Then
Cells(4, 5) = 0
If Cells(3, i) > 999999 Then *****needs to elseif
Cells(4, 4) = 500000
ElseIf Cells(3, i) < 999999 Then
Cells(4, 4) = Cells(3, i) - 499999
End If
在最后一个if blcok
中,您的代码类型不匹配