Sub HarvestSequenceOptimizer()
Dim rw As Long
Dim L As Integer
Dim M As Integer
Dim N As Integer
With ActiveWorkbook.ActiveSheet
LastRow = .UsedRange.Rows.Count
For rw = 4 To LastRow
.Cells(rw, 15).Select 'Select Cell in row with Start date "O"
If .Cells(rw, 15) < .Cells(rw, 28) And .Cells(rw + 1, 15) >= .Cells(rw + 1, 28) Then 'If, in selected row, Start "O" < Not Before "AB" and in next row Start "O" >= Not Before "AB"
Do Until .Cells(rw, 15) >= .Cells(rw, 28) 'Until, in selected row Start "O" >= Not Before "AB"
.Cells(rw, 15).EntireRow.Select 'Select row
Selection.Cut 'Cut row
.Cells(rw + 2, 15).EntireRow.Select 'Select 2 rows down
Selection.Insert Shift:=xlDown 'Insert previously cut row
Loop
Call ComputeStartCutDateandHarvestAge 'Re-calculate Start date and Age as from Row Number 4 by copying formula in Row Number 3 to Row Number 4 upto last row
ElseIf .Cells(rw, 15) < .Cells(rw, 28) And .Cells(rw + 1, 15) < .Cells(rw + 1, 28) Then 'If, in both selected row and next row, Start "O" < Not Before "AB"
Do Until .Cells(rw + L + 1, 15) >= .Cells(rw + L + 1, 28) 'Until, Start "O" >= Not Before "AB" in row after loop
Rows(rw & ":" & rw + L).Select 'Select all rows until Start "O" >= Not Before "AB" in row after loop
Selection.Cut 'Cut all rows in loop
.Cells(rw + L + 2, 15).EntireRow.Select 'Ínsert all selected rows under the row whereby Start Date "O" >= Not Before "AB" after loop
Selection.Insert Shift:=xlDown
L = L + 1 'Count Number of Loops
Loop
Call ComputeStartCutDateandHarvestAge 'Re-calculate Start date and Age as from Row Number 4 by copying formula in Row Number 3 to Row Number 4 upto last row
End If
If .Cells(rw, 22) < 11 And .Cells(rw + 1, 22) >= 11 Then 'If, in selected row, Age "V" < 11 and in next row Age "V" >= 11
Do Until .Cells(rw, 22) >= 11 'Ín selected row Age "V" >= 11
.Cells(rw, 22).EntireRow.Select 'Select entire row
Selection.Cut 'Cut row
.Cells(rw + 2, 22).EntireRow.Select 'Select 2 rows down
Selection.Insert Shift:=xlDown 'Insert previously cut row
Loop
Call ComputeStartCutDateandHarvestAge 'Re-calculate Start date and Age as from Row Number 4 by copying formula in Row Number 3 to Row Number 4 upto last row
ElseIf .Cells(rw, 22) < 11 And .Cells(rw + 1, 22) < 11 Then 'If, in both selected row and next row Age "V" < 11
Do Until .Cells(rw + M + 1, 22) >= 11 'Until, Age "V" >= 11 in row after loop
Rows(rw & ":" & rw + M).Select 'Select all rows until Age "V" >= 11 in row after loop
Selection.Cut 'Cut all rows in loop
.Cells(rw + M + 2, 22).EntireRow.Select 'Ínsert all selected rows under the row whereby Age "V" >= 11 after loop
Selection.Insert Shift:=xlDown
M = M + 1 'Count Number of Loops
Loop
Call ComputeStartCutDateandHarvestAge 'Re-calculate Start date and Age as from Row Number 4 by copying formula in Row Number 3 to Row Number 4 upto last row
End If
If .Cells(rw, 15) > .Cells(rw, 29) And .Cells(rw + 1, 15) <= .Cells(rw + 1, 29) Then 'If, in selected row, Start "O" > Not After "AC" and in next row Start "F" <= Not After "P"
Do Until .Cells(rw, 15) <= .Cells(rw, 29) 'Until, in selected row Start "O" <= Not After "AC"
.Cells(rw, 5).EntireRow.Select 'Select entire row
Selection.Cut 'Cut row
.Cells(rw - 1, 5).EntireRow.Select 'Select 2 rows above
Selection.Insert Shift:=xlDown 'Insert previously cut row 2 rows above
Loop
Call ComputeStartCutDateandHarvestAge 'Re-calculate Start date and Age as from Row Number 4 by copying formula in Row Number 3 to Row Number 4 upto last row
ElseIf .Cells(rw, 15) < .Cells(rw, 29) And .Cells(rw + 1, 15) < .Cells(rw + 1, 29) Then 'If, in both selected row and next row Start "O" > Not After "AC"
Do Until .Cells(rw + N + 1, 15) <= .Cells(rw + N + 1, 29) 'Until, Start "O" <= Not After "AC" in row after loop
Rows(rw & ":" & rw + L).Select 'Select all rows until Start "O" <= Not After "AC" in row after loop
Selection.Cut 'Cut all rows in loop
.Cells(rw - 1, 15).EntireRow.Select 'Select 2 rows above
Selection.Insert Shift:=xlDown 'Insert previously cut row 2 rows above
N = N + 1 'Count Number of Loops
Loop
Call ComputeStartCutDateandHarvestAge 'Re-calculate Start date and Age as from Row Number 4 by copying formula in Row Number 3 to Row Number 4 upto last row
End If
Next rw
Dim OutPut As Integer
OutPut = MsgBox("The processing is complete.", vbInformation, "Harvest Sequence Optimizer")
End With
End Sub