问题
I keep getting errors (either else without if or loop without do) in this, and I really have no idea why... can anyone help?! Thanks!
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
回答1:
Try replacing the code below with what you have. You were missing Next
for each of your For
loops. Do
and Loop
go together, similarly For
and Next
go together (they form the basis for the loop).
Here's the code, I've cleaned this up as well to make it easier to follow.
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
回答2:
You are missing Next
statement for all you For
loops
Try below code
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
回答3:
This is why proper and consistent indentation matters:
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
Now, you have 6 levels of nesting here, and quite much duplication. That's a sign you need to refactor and extract a procedure out of the inner nested code, to eliminate redundancies and improve the code's readability and maintainability (one modification should mean one single place to change the code).
If your code works as intended, I'd suggest you bring it (the whole procedure, or even the entire module!) over to Code Review for a cleanup and tips to improve and solidity your code (e.g. you probably don't need to Calculate
as often as you do, and unqualified Range
calls implicitly refer to the active worksheet - and this might cause unexpected bugs later!).
来源:https://stackoverflow.com/questions/39042710/syntax-errors-in-vba