VBA скрипт отличается от задания
Sub calculateSum()
Dim rng As Range
Dim lastRow As Long, category As String, cost As Long, totalCost As Long, nextCost As Long
Dim i As Integer, j As Integer
Sheets(5).Activate
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
category = Cells(i, 2)
cost = Cells(i, 4)
totalCost = totalCost + cost
Cells(i, 5) = 0
'Case : 1 Cost more than 800
If totalCost > 800 Then
Cells(i, 5) = cost
totalCost = 0
Else
'Case : 2 Cost is less than 800
If category = Cells(i + 1, 2) Then
If (totalCost + Cells(i + 1, 4)) > 800 Then
Cells(i, 5) = totalCost
totalCost = 0
End If
Else
Cells(i, 5) = totalCost
totalCost = 0
End If
End If
Next i
End Sub