Sub calc_subtotals()
Dim str As String
Dim lastrow As Long
Dim i As Long
Dim rng As Range
Dim celle As Range
Dim str1 As String
Dim str2 As String
Dim rowe As Long
Dim temp As Double
Application.ScreenUpdating = False
rowe = 2
str1 = "A"
str2 = "E"
With Sheets("Sheet1")
Set rng = Range(.Cells(rowe, str1), .Cells(.Cells.Rows.Count, str2).End(xlUp))
End With
rng.Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range("B2") _
, Order2:=xlAscending, Key3:=Range("E2"), Order3:=xlAscending, Header:= _
xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
xlSortNormal
str = "B"
With Sheets("Sheet1")
lastrow = .Cells(.Cells.Rows.Count, str).End(xlUp).Offset(1, 0).Row
For i = lastrow To 3 Step -1
If .Cells(i, "B") <> .Cells(i - 1, "B") Then
.Rows(i).Insert Shift:=xlDown
End If
Next i
str = "B"
lastrow = .Cells(.Cells.Rows.Count, str).End(xlUp).Offset(1, 0).Row
temp = 0
For i = 2 To lastrow
temp = temp + Cells(i, 5)
If Cells(i, 5) = "" Then
Cells(i, 6) = temp
temp = 0
Cells(i, 1) = "Sub-total"
End If
Next i
End With
Columns("F:F").NumberFormat = "#,##0.00"
Application.ScreenUpdating = True
End Sub
|