UPDATE: After mucking around these forum posts, I managed to populate the desired records using the following macro code. I would love to receive feedback on making this code more efficient/elegant or maintenance friendly. I have attached the working Excel spreadsheet for ease of following my logic.
Sub recordFD()
Dim fdRef As String
Dim fdBank As String
Dim fdDate As Date
Dim fdPayout As Long
Dim fdPeriod As Long
Dim fdAmount As Long
Dim fdRate As Double
Dim fdID As Long
'Move the range values to local variables
With Worksheets("Input")
fdRef = Range("FD_REF").Value
fdBank = Range("FD_BANK").Value
fdDate = Range("FD_START").Value
fdPayout = Range("FD_FREQ").Value
fdPeriod = Range("FD_PERIOD").Value
fdAmount = Range("FD_AMOUNT").Value
fdRate = Range("FD_RATE").Value
End With
'Clear the input range as a feedback to the user.
Range("C3:C9").ClearContents
Dim lastRow As Long
Dim eRow As Long
'Select the FD_Master worksheet and find the last populated row number
Worksheets("FD_Master").Select
With Worksheets("FD_Master")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Calculate the row number for the entry of records
eRow = lastRow + 1
fdID = lastRow
Range("A" & eRow).Value = lastRow
Range("B" & eRow).Value = fdBank
Range("C" & eRow).Value = fdDate
Range("D" & eRow).Value = fdPeriod
Range("E" & eRow).Value = fdPayout
Range("F" & eRow).Value = fdAmount
Range("G" & eRow).Value = fdRate
Range("H" & eRow).Value = fdRef
End With
Dim i As Long
Dim nextPaymentOn As Date
Dim lastPayDate As Date
Dim tPayouts As Long
lastRow = 0
eRow = 0
'Select the Payment_Master worksheet and find the last populated row number
Worksheets("Payment_Master").Select
With Worksheets("Payment_Master")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
tPayouts = fdPeriod / fdPayout
lastPayDate = fdDate
For i = 1 To tPayouts
'Calculate the row number for the entry of records
eRow = lastRow + 1
Range("A" & eRow).Value = lastRow
Range("B" & eRow).Value = fdID
nextPaymentOn = nextPayDate(lastPayDate, fdPayout)
Range("C" & eRow).Value = nextPaymentOn
lastPayDate = nextPaymentOn
Range("D" & eRow).Value = fdAmount * fdRate / tPayouts
Range("E" & eRow).Value = "No"
'Increment the lastRow counter
lastRow = lastRow + 1
Next i
End With
End Sub
Public Function nextPayDate(fdLastPayout As Date, fdPeriod As Long) As Date
nextPayDate = DateAdd("m", fdPeriod, fdLastPayout)
End Function
Bookmarks