Try this macro
Sub Test()
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Columns("A:A") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Columns("B:B") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A:D")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("A:A").Copy Destination:=Columns("K:K")
Columns("K:K").RemoveDuplicates Columns:=1, Header:=xlYes
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Columns("K:K") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Columns("K:K")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For N = 2 To Cells(Rows.Count, 11).End(xlUp).Row
Cells(Rows.Count, 13).End(xlUp).Offset(2, 0) = Cells(N, 11)
For M = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(M, 1) = Cells(N, 11) Then
If Cells(M, 2) <> Cells(M - 1, 2) Or Cells(M, 1) <> Cells(M - 1, 1) Then
Cells(Rows.Count, 13).End(xlUp).Offset(1, 0) = Cells(M, 2)
Cells(Rows.Count, 13).End(xlUp).Offset(0, 1) = WorksheetFunction.SumIfs(Columns(3), Columns(1), Cells(Rows.Count, 13).End(xlUp).End(xlUp), Columns(2), Cells(Rows.Count, 13).End(xlUp))
Cells(Rows.Count, 13).End(xlUp).Offset(0, 2) = WorksheetFunction.SumIfs(Columns(4), Columns(1), Cells(Rows.Count, 13).End(xlUp).End(xlUp), Columns(2), Cells(Rows.Count, 13).End(xlUp))
End If
End If
Next M
Next N
End Sub
Open up the VBA editor by hitting ALT F11
Insert a new module by hitting Insert - Module
Paste the macro into the empty sheet
Hit ALT F11 to get back to the worksheet.
Run the macro by going to tools-macro in Excel 2003 or the view ribbon in Excel 2007/2010.
Bookmarks