Sub FillCashFlow()
' Dim Cell As Object
Dim Duration As Integer
Dim Budget As Long
Dim MthExp As Long
Dim Mth1 As Long
Dim MthLast As Long
Dim Retention As Long
Dim StartDate As Date
Dim FinishDate As Date
Dim PramA As Integer
Dim PramB As Integer
Dim Period As Integer
Dim ToDate As Range
Dim StartMth As Range
Dim EndMth As Range
Dim ContractExp As Long
Dim Loading As String
' Dim Selection As String
Dim Project As Range
'Application.ScreenUpdating = False
'Clear Previous
'Range("F2:AC8").Select
'Selection.ClearContents
'Selection.Interior.Pattern = xlNone
'Selection.Interior.TintAndShade = 0
'Selection.Style = "Comma"
'Range("A2").Select
' Set Do loop to stop when an empty cell is reached.
' Do Until IsEmpty(ActiveCell)
'Selection = ActiveWindow.RangeSelection.Address
For Each Project In Selection.Rows
'ProjectSelected = Projects.Row
Budget = 0
Duration = 0
StartDate = 0
FinishDate = 0
Loading = ""
'Get Start Date
Cells(ActiveCell.Row, 1).Select
Do
If Cells(1, ActiveCell.Column) = "Start Date" Then StartDate = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop Until StartDate > 0
'Get Finish Date
Cells(ActiveCell.Row, 1).Select
Do
If Cells(1, ActiveCell.Column) = "Finish Date" Then FinishDate = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop Until FinishDate > 0
'Calculate Project Duration
Duration = DateDiff("M", StartDate, FinishDate) + 1
'Get Budget
Cells(ActiveCell.Row, 1).Select
Do
If Cells(1, ActiveCell.Column) = "Budget" Then Budget = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop Until Budget > 0
ContractExp = Budget * 0.8
MthExp = (Budget * 0.8) / Duration
Mth1 = MthExp + Budget * 0.1
'Get Loading
Cells(ActiveCell.Row, 1).Select
Do
If Cells(1, ActiveCell.Column) = "Loading" Then Loading = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop Until Not Loading = ""
'MsgBox Loading
If Loading = "F" Then PramA = 1 And PramB = 0
'If Loading = "M" Then PramA = 0 And PramB = 1
If Loading = "B" Then PramA = 0 And PramB = 0 Else: PramA = 0 And PramB = 1
MsgBox PramA & PramB
'Start at Start Date
Cells(ActiveCell.Row, 1).Select
Do
ActiveCell.Offset(0, 1).Select
If Cells(1, ActiveCell.Column) = StartDate Then _
ActiveWorkbook.Names.Add Name:="StartMth", RefersToR1C1:=ActiveCell
'If Cells(1, ActiveCell.Column) = StartDate Then ActiveCell.Interior.Color = 65535
If Cells(1, ActiveCell.Column) = StartDate Then ActiveCell.Value = 0
Loop Until Cells(1, ActiveCell.Column) = StartDate
Period = 0
Duration = Duration - 1
ActiveCell.Offset(0, 1).Select
Do
Period = Period + 1
'MsgBox Application.Sum(Range("StartMth"), "ActiveCell.Offset(0, -1))")
ActiveCell.Value = ((10 * (Period / Duration) ^ 2 * (1 - (Period / Duration)) ^ 2 _
* (0 + 1 * (Period / Duration)) + (Period / Duration) ^ 4 _
* (5 - 4 * (Period / Duration))) * ContractExp) - WorksheetFunction.Sum(Range("StartMth", ActiveCell.Offset(0, -1)))
ActiveCell.Offset(0, 1).Select
Loop Until Period = Duration
ActiveCell.Offset(0, -1).Select
ActiveWorkbook.Names.Add Name:="EndMth", RefersToR1C1:=ActiveCell
'MsgBox Application.Sum("Range(StartMth)": Range("EndDate"))
'Pay Advance Payment
Application.Goto Reference:="StartMth"
ActiveCell.Value = ActiveCell.Value + (Budget * 0.1)
'Release 1st Half Retention
Application.Goto Reference:="EndMth"
ActiveCell.Value = ActiveCell.Value + (Budget * 0.05)
'Release 2nd half Retention
ActiveCell.Offset(0, 12).Select
'Selection.Interior.Color = 65535
ActiveCell.Value = Budget * 0.05
' MsgBox "Start Date " & StartDate & vbNewLine & "Finish Date " & FinishDate & vbNewLine & "Budget " & Budget
' Cells(ActiveCell.Row, 1).Select
' ActiveCell.Offset(1, 0).Select
'Loop
Next Project
' Application.ScreenUpdating = True
End Sub