Sub f()
Dim i As Long, lr As Long, k As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Cells(lr + 1, 1) = "ИТОГО"
Cells(lr + 2, 1) = "ВСЕГО"
For i = lr To 3 Step -1
If Cells(i, 7) = Cells(i - 1, 7) And Cells(i, 8) <> Cells(i - 1, 8) Then
Rows(i & ":" & i + 1).Insert
Cells(i + 1, 1) = Cells(i + 2, 8)
Cells(i, 1) = "ИТОГО"
ElseIf Cells(i, 7) <> Cells(i - 1, 7) Then
Rows(i & ":" & i + 3).Insert
Cells(i, 1) = "ИТОГО"
Cells(i + 1, 1) = "ВСЕГО"
Cells(i + 2, 1) = Cells(i + 4, 7)
Cells(i + 3, 1) = Cells(i + 4, 8)
Else
End If
Next i
lr = Cells(Rows.Count, 1).End(xlUp).Row
For k = lr To 3 Step -1
If Cells(k, 1) = "ВСЕГО" Then
Cells(k, 6) = Application.WorksheetFunction.SumIf(Range("G:G"), Cells(k - 2, 7), Range("F:F"))
ElseIf Cells(k, 1) = "ИТОГО" Then
Cells(k, 6) = Application.WorksheetFunction.SumIfs(Range("F:F"), Range("G:G"), Cells(k - 2, 7), Range("H:H"), Cells(k - 2, 8))
End If
Next k
Range("A3" & ":H" & lr).Borders.LineStyle = xlContinuous
End Sub
|