Для этого ответа я использую Лист1 (включает Первичные данные)
и Лист 2 (включает итоги)
Попробуйте:
Option Explicit
Sub test()
Dim i As Long, j As Long, LR1 As Long, LR2 As Long
Dim Segment As String
Dim Total As Double
Dim Exist As Boolean
LR1 = Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row
For i = 2 To LR1
Exist = True
Segment = Sheet1.Range("B" & i).Value
Total = Application.WorksheetFunction.SumIf(Sheet1.Range("B2:B" & LR1), Sheet1.Range("B" & i).Value, Sheet1.Range("D2:D" & LR1))
LR2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
If LR2 = 1 Then
Sheet2.Range("A2").Value = Segment
Sheet2.Range("A2").Offset(0, 1).Value = Total
Else
LR2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
For j = 2 To LR2
If Sheet2.Range("A" & j).Value <> Segment Then
Exist = False
Else
Exist = True
Exit For
End If
Next j
If Exist = False Then
Sheet2.Range("A" & LR2 + 1).Value = Segment
Sheet2.Range("A" & LR2 + 1).Offset(0, 1).Value = Total
End If
End If
Next i
End Sub