Try:
Sub test()
Dim i As Long, j As Long
Const startcolumn As Long = 4 ' starting with column D to allow changes in column C
Const endcolumn As Long = 12 ' ending with column L
Const maxallowedamount As Currency = 50000000
i = startcolumn
Do
If Cells(27, i).Value <= maxallowedamount Then
i = i + 1 'look at next column in row 27
Else 'search for first value to change in row 24
j = i - 1
While Cells(24, j).Value = 0 And j >= startcolumn - 1
j = j - 1
Wend
If j < startcolumn - 1 Then
' sorry no chance to correct
MsgBox "Could not correct value in cell: " & Cells(27, i).Address(0, 0), vbCritical, "Giving up :-("
i = endcolumn + 1
ElseIf Cells(27, i).Value - maxallowedamount >= Cells(24, j).Value Then
Cells(24, j).Value = 0
Else
Cells(24, j).Value = Cells(24, j).Value - (Cells(27, i).Value - maxallowedamount)
End If
End If
Loop Until i > endcolumn
End Sub
Bookmarks