This is why correct 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
You now have 6 breeding levels and quite a lot of duplication. This is a sign necessary for the reorganization and extraction of the procedure from the internal embedded code, to eliminate redundancy and improve the readability of the code and maintainability (one modification should mean one place for changing the code).
If your code works as intended, I suggest you bring it (the whole procedure or even the whole module!) To the Code Review for cleaning and tips for improving and maintaining the integrity of your code (for example, you probably don't need Calculate as often as you do, and unqualified Range calls implicitly reference the active sheet - and this can lead to unexpected errors later!).
source share