HI Everyone,
I have some vba code that I’ve been using for years that I use to project the recoveries of an asset over some period of time according to a vector. So if I have cash outlay in period 1, I have a curve that recovers that cash amount over some period time (let’s say 24 months). In period 1, I would multiply the first value in the 24 month vector by the initial cash amount so that 100% is recovered in month 24. If there is another cash outlay in period 2 then that cash would also be recovered according to the same curve. So in period 2, you multiply the Cash Amount from Period 1 by the 2nd value in the vector…then the Cash Amount from Period 2 would be multiplied by the 1st value in the vector and so forth.
This code works perfect for that. The issue is the code can become very cumbersome. I’m hoping someone can help me reduce the repetitive parts of the code…I’m about to have 100s of different curves and cash amounts so typing in 100 different arrays is going to be painful.
I can't seem to be able to attach the spreadsheet. In its current form the spreadsheet just has 5 recovery curves and asset amounts. All of the recovery curves are next to each other. The cash amounts are in columns next to those and the results are printed in the columns to the right of the cash amounts. So 1st 5 columns are the recovery curves, next 5 columns represent different types of cash flows that occur in each period. (the curves start in column B – I reserve the first column for the Month so in row 7, column A, is “1”, in the next row in column A is the number “2”…so 1,2,3,4…
I have some vba code that I’ve been using for years that I use to project the recoveries of an asset over some period of time according to a vector. So if I have cash outlay in period 1, I have a curve that recovers that cash amount over some period time (let’s say 24 months). In period 1, I would multiply the first value in the 24 month vector by the initial cash amount so that 100% is recovered in month 24. If there is another cash outlay in period 2 then that cash would also be recovered according to the same curve. So in period 2, you multiply the Cash Amount from Period 1 by the 2nd value in the vector…then the Cash Amount from Period 2 would be multiplied by the 1st value in the vector and so forth.
This code works perfect for that. The issue is the code can become very cumbersome. I’m hoping someone can help me reduce the repetitive parts of the code…I’m about to have 100s of different curves and cash amounts so typing in 100 different arrays is going to be painful.
I can't seem to be able to attach the spreadsheet. In its current form the spreadsheet just has 5 recovery curves and asset amounts. All of the recovery curves are next to each other. The cash amounts are in columns next to those and the results are printed in the columns to the right of the cash amounts. So 1st 5 columns are the recovery curves, next 5 columns represent different types of cash flows that occur in each period. (the curves start in column B – I reserve the first column for the Month so in row 7, column A, is “1”, in the next row in column A is the number “2”…so 1,2,3,4…
Code:
Sub Recovery()
Dim myArray
Dim cfArray1() As Double: Dim cfArray2() As Double: Dim cfArray3() As Double
Dim cfArray4() As Double: Dim cfArray5() As Double
Dim fr As Long: Dim lr As Long: Dim X As Long: Dim i As Long: Dim p As Long
Dim C1 As Long: Dim C2 As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
fr = 7 'first data row
lr = 500 'last row of data
C1 = 2 '1st column of recovery curves (first column is for Months/Period so row 7 = period 1; Row 8 = Period 2, etc...)
C2 = 7 '1st column of assets
myArray = Worksheets("Recovery").Range("A1:P" & lr).Value
ReDim cfArray1(fr To lr, 1): ReDim cfArray2(fr To lr, 1): ReDim cfArray3(fr To lr, 1)
ReDim cfArray4(fr To lr, 1): ReDim cfArray5(fr To lr, 1)
For X = fr To lr
p = fr
For i = X To X - (lr - fr + 1) Step -1
If i < fr Then Exit For
cfArray1(X, 0) = cfArray1(X, 0) + myArray(i, C1 + 0) * myArray(p, C2 + 0)
cfArray2(X, 0) = cfArray2(X, 0) + myArray(i, C1 + 1) * myArray(p, C2 + 1)
cfArray3(X, 0) = cfArray3(X, 0) + myArray(i, C1 + 2) * myArray(p, C2 + 2)
cfArray4(X, 0) = cfArray4(X, 0) + myArray(i, C1 + 3) * myArray(p, C2 + 3)
cfArray5(X, 0) = cfArray5(X, 0) + myArray(i, C1 + 4) * myArray(p, C2 + 4)
p = p + 1
Next i
Next X
Worksheets("Recovery").Cells(fr, "L").Resize(UBound(cfArray1, 1) - LBound(cfArray1, 1) + 1) = cfArray1
Worksheets("Recovery").Cells(fr, "M").Resize(UBound(cfArray2, 1) - LBound(cfArray2, 1) + 1) = cfArray2
Worksheets("Recovery").Cells(fr, "N").Resize(UBound(cfArray3, 1) - LBound(cfArray3, 1) + 1) = cfArray3
Worksheets("Recovery").Cells(fr, "O").Resize(UBound(cfArray4, 1) - LBound(cfArray4, 1) + 1) = cfArray4
Worksheets("Recovery").Cells(fr, "P").Resize(UBound(cfArray5, 1) - LBound(cfArray5, 1) + 1) = cfArray5
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Last edited by a moderator: