Sub Prog06()
Dim LastRow As Long
Dim a As Long
Dim Row2 As Long
Dim Row As Long
Dim Row3 As Long
Row = 3
Row2 = 3
Row3 = 3
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A2:D" & LastRow).Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"A3:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"B3:B" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A2:D" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Do Until IsEmpty(Cells(Row, 1))
If Cells(Row, 2) = Cells(Row + 1, 2) And Cells(Row, 1) = Cells(Row + 1, 1) Then
Cells(Row2, 11) = Cells(Row, 1)
Cells(Row2, 12) = Cells(Row, 2)
Cells(Row2, 13) = Cells(Row, 3)
Cells(Row2, 14) = Cells(Row, 4)
Else
Cells(Row2, 11) = Cells(Row, 1)
Cells(Row2, 12) = Cells(Row, 2)
Cells(Row2, 13) = Cells(Row, 3)
Cells(Row2, 14) = Cells(Row, 4)
Cells(Row2 + 1, 13).Formula = "=sum(M3:M" & Row2 & ")"
If Cells(Row2 + 1, 13) = 0 Then
Range("K3:N" & Row2).Select
Selection.Copy
Range("F" & Row3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K3:N" & Row2 + 1).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Row3 = Cells(Rows.Count, "F").End(xlUp).Row + 1
Row2 = 2
Else
Range("K3:N" & Row2 + 1).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Row2 = 2
End If
End If
Row2 = Row2 + 1
Row = Row + 1
Range("A2").Select
Loop
Range("A2").Select
End Sub