Sub x()
Dim r As Long, c As Long
Const n As Long = 160
Sheet2.Activate
For r = 2 To Cells(Rows.Count, 1).End(xlUp).Row
c = 1
Do While WorksheetFunction.Sum(Cells(r, 4).Resize(, c)) < Cells(r, 1)
If Cells(r, 1) - WorksheetFunction.Sum(Cells(r, 4).Resize(, c)) < n Then
Cells(r, 3 + c) = Cells(r, 1) - WorksheetFunction.Sum(Cells(r, 4).Resize(, c))
Else
Cells(r, 3 + c) = n
End If
c = c + 1
Loop
Next r
End Sub
Bookmarks