我不断收到错误(其他没有if或循环没有做到这一点),我真的不知道为什么......有人可以帮忙吗?!谢谢!
Do Until (Range("I4").Value = 0)
For i = 2 To lLastrow
If Range("G" & i).Value = 0 Then
i = i + 1
ElseIf Range("G" & i).Value < 0 Then
Do Until (Range("G" & i).Value = 0)
For j = 0 To i
If Range("F" & i - j).Value < 0 Then
Range("F" & i - j).Value = Range("F" & i - j).Value + 1
Else: j = j + 1
End If
Application.Calculate
Loop
ElseIf Range("G" & i).Value > 0 Then
Do Until (Range("G" & i).Value = 0)
For k = 0 To i
If Range("F" & i - k).Value > 0 Then
Range("F" & i - k).Value = Range("F" & i - k).Value - 1
Else: k = k + 1
End If
Application.Calculate
Loop
End If
Application.Calculate
Loop
答案 0 :(得分:4)
尝试使用您拥有的代码替换下面的代码。您的每个Next
循环都缺少For
。 Do
和Loop
一起使用,类似For
和Next
一起使用(它们构成了循环的基础)。
这是代码,我已经清理了这个代码,以便更容易理解。
Do Until (Range("I4").Value = 0)
For i = 2 To lLastrow
If Range("G" & i).Value = 0 Then
i = i + 1
ElseIf Range("G" & i).Value < 0 Then
Do Until (Range("G" & i).Value = 0)
For j = 0 To i
If Range("F" & i - j).Value < 0 Then
Range("F" & i - j).Value = Range("F" & i - j).Value + 1
Else
j = j + 1
End If
Application.Calculate
Next j
Loop
ElseIf Range("G" & i).Value > 0 Then
Do Until (Range("G" & i).Value = 0)
For k = 0 To i
If Range("F" & i - k).Value > 0 Then
Range("F" & i - k).Value = Range("F" & i - k).Value - 1
Else
k = k + 1
End If
Application.Calculate
Next k
Loop
End If
Next i
Application.Calculate
Loop
答案 1 :(得分:1)
您缺少所有Next
循环的For
语句
尝试以下代码
Do Until (Range("I4").Value = 0)
For i = 2 To lLastrow
If Range("G" & i).Value = 0 Then
i = i + 1
ElseIf Range("G" & i).Value < 0 Then
Do Until (Range("G" & i).Value = 0)
For j = 0 To i
If Range("F" & i - j).Value < 0 Then
Range("F" & i - j).Value = Range("F" & i - j).Value + 1
Else
j = j + 1
End If
Application.Calculate
Next'You missed this
Loop
ElseIf Range("G" & i).Value > 0 Then
Do Until (Range("G" & i).Value = 0)
For k = 0 To i
If Range("F" & i - k).Value > 0 Then
Range("F" & i - k).Value = Range("F" & i - k).Value - 1
Else
k = k + 1
End If
Application.Calculate
Next 'You missed this
Loop
End If
Application.Calculate
Next 'You missed this
Loop
答案 2 :(得分:1)
这就是为什么正确的和一致的缩进很重要的原因:
Do Until (Range("I4").Value = 0)
| For i = 2 To lLastrow
| | If Range("G" & i).Value = 0 Then
| | | i = i + 1
| | ElseIf Range("G" & i).Value < 0 Then
| | | Do Until (Range("G" & i).Value = 0)
| | | | For j = 0 To i
| | | | | If Range("F" & i - j).Value < 0 Then
| | | | | | Range("F" & i - j).Value = Range("F" & i - j).Value + 1
| | | | | Else
| | | | | | j = j + 1
| | | | | End If
| | | | | Application.Calculate
| | | | Next '<<<<<<< MISSING!!
| | | Loop
| | ElseIf Range("G" & i).Value > 0 Then
| | | Do Until (Range("G" & i).Value = 0)
| | | | For k = 0 To i
| | | | | If Range("F" & i - k).Value > 0 Then
| | | | | | Range("F" & i - k).Value = Range("F" & i - k).Value - 1
| | | | | Else
| | | | | | k = k + 1
| | | | | End If
| | | | | Application.Calculate
| | | | Next '<<<<<<< MISSING!!
| | | Loop
| | End If
| | Application.Calculate
| Next '<<<<<<< MISSING!!
Loop
现在,你在这里有6个级别的嵌套,并且有很多重复。您需要重构并从内部嵌套代码中提取程序,以消除冗余并提高代码的可读性和可维护性(一个修改应表示一个地方来更改代码。)
如果您的代码按预期工作,我建议您将其(整个过程,甚至整个模块!)带到Code Review进行清理,并提示改进和巩固您的代码(例如,您可能不需要经常Calculate
,并且不合格的Range
调用会隐式引用活动工作表 - 这可能会在以后导致意外错误!)。