OK, give this a try
Sub x()
Dim r As Long, c As Long, rData As Range, i
Const n As Long = 160
Sheet2.Activate
Set rData = Range("A1").CurrentRegion
For r = 2 To rData.Rows.Count
i = Application.Match(CLng(DateSerial(Year(Cells(r, 2)), Month(Cells(r, 2)), 1)), rData.Rows(1), 0)
If IsNumeric(i) Then
c = 1
Do While WorksheetFunction.Sum(Cells(r, i).Resize(, c)) < Cells(r, 1)
If Cells(r, 1) - WorksheetFunction.Sum(Cells(r, i).Resize(, c)) < n Then
Cells(r, i - 1 + c) = Cells(r, 1) - WorksheetFunction.Sum(Cells(r, i).Resize(, c))
Else
Cells(r, i - 1 + c) = n
End If
c = c + 1
Loop
End If
Next r
With rData
.Offset(1, 3).Resize(.Rows.Count - 1, .Columns.Count - 3).SpecialCells(xlCellTypeBlanks).Value = 0
End With
End Sub
Bookmarks