Try this code and let me know. Put it in a new module of your workbook and run it.
Sub group_data()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
ws1.Columns("A:I").Select
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Key2:=Range("G2") _
, Order2:=xlAscending, Key3:=Range("I2"), Order3:=xlAscending, Header:= _
xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
xlSortNormal
lastrow = ws1.Range("A1").End(xlDown).Row
ws1.Rows("1:1").Copy
ws2.Select
Range("A1").Select
ActiveSheet.Paste
lrow = ws2.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If ws1.Range("D" & i).Value = ws1.Range("D" & i + 1).Value Then
If ws1.Range("E" & i).Value = ws1.Range("E" & i + 1).Value Then
If ws1.Range("G" & i).Value = ws1.Range("G" & i).Value Then
proceed:
qtysum = qtysum + ws1.Range("I" & i).Value
If qtysum > 12 Then
lrow = lrow + 1
qtysum = 0
i = i - 1
Else
ws1.Rows(i & ":" & i).Cut
ws2.Activate
Range("A" & lrow + 1).Select
ActiveSheet.Paste
ws1.Rows(i & ":" & i).Delete
lastrow = lastrow - 1
lrow = lrow + 1
i = i - 1
End If
End If
End If
Else
GoTo proceed
End If
Next
End Sub
Let me know if you face any issues.
Bookmarks