Private Sub Worksheet_Change(ByVal Target As Range)
Dim lRow%, sum
lRow = [A65536].End(xlUp).Row
On Error Resume Next
sum = 0
If Target.Row = 2 Then
For i = 3 To lRow
sum = sum + Cells(i, 1)
If sum >= Target.Value Then
If sum > Target.Value Then
Cells(i, Target.Column) = Target.Value - (sum - Cells(i, 1))
For j = 3 To i - 1
Cells(j, Target.Column) = Cells(j, 1)
Next
ElseIf sum = Target.Value Then
For j = 3 To i
Cells(j, Target.Column) = Cells(j, 1)
Next
End If
Exit For
End If
Next
End If
End Sub
https://www.zhihu.com/video/1116645860737466368