# Recovery Curve VBA Code help to reduce repetitive Parts

#### dariushou

##### Board Regular
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…

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:

#### dariushou

##### Board Regular
I couldn't attached the spreadsheet, but here is the data in excel in table format. the data starts on row 7 so Month 1 is row 7.

 Totals 100.00% 100.00% 100.00% 100.00% 100.00% 888,109 222,027 1,110,136 1,332,164 417,411 888,109 222,027 1,110,136 1,332,164 417,411 Month Curve 1 Curve 2 Curve 3 Curve 4 Curve 5 Assets 1 Assets 2 Assets 3 Assets 4 Assets 5 Recovery 1 Recovery 2 Recovery 3 Recovery 4 Recovery 5 1 6.0000% 16.0000% 24.0000% 2.0000% 4.5000% 25,000 6,250 31,250 37,500 11,750 1,500.00 1,000.00 7,500.00 750.00 528.75 2 3.0000% 2.0000% 1.0000% 3.2000% 2.9500% 35,000 8,750 43,750 52,500 16,450 2,850.00 1,525.00 10,812.50 2,250.00 1,086.88 3 4.0000% 3.0000% 2.0000% 4.2000% 3.9500% 14,000 3,500 17,500 21,000 6,580 2,890.00 922.50 5,262.50 3,675.00 1,245.50 4 5.0000% 4.0000% 3.0000% 5.2000% 4.9500% 10,000 2,500 12,500 15,000 4,700 3,670.00 982.50 4,987.50 5,127.00 1,637.01 5 2.0000% 1.0000% 0.0000% 2.2000% 1.9500% 50,000 12,500 62,500 75,000 23,500 6,110.00 2,567.50 16,787.50 6,417.00 2,499.46 6 1.0000% 0.0000% 1.0000% 3.2000% 2.9500% 125,000 31,250 156,250 187,500 58,750 11,050.00 5,552.50 39,212.50 10,227.00 4,515.76 7 8.0000% 7.0000% 6.0000% 8.2000% 7.9500% 350,000 87,500 437,500 525,000 164,500 29,880.00 15,572.50 110,500.00 25,647.00 11,844.24 8 12.0000% 11.0000% 10.0000% 12.2000% 11.9500% 24,000 6,000 30,000 36,000 11,280 25,580.00 5,472.50 22,500.00 39,177.00 11,841.89 9 13.0000% 12.0000% 11.0000% 13.2000% 12.9500% 69,000 17,250 86,250 103,500 32,430 34,780.00 8,837.50 43,425.00 50,229.00 15,805.40 10 25.0000% 24.0000% 23.0000% 25.2000% 24.9500% 88,888 22,222 111,110 133,332 41,777 42,143.28 11,003.02 56,378.90 61,487.64 19,485.71 11 21.0000% 20.0000% 19.0000% 21.2000% 20.9500% 7,777 1,944 9,721 11,666 3,655 36,363.26 7,308.02 30,556.70 60,200.93 18,025.05 12 444 111 555 666 209 39,395.47 7,925.81 38,752.61 71,887.56 21,620.93 13 25,000 6,250 31,250 37,500 11,750 62,828.80 14,484.43 66,283.28 95,680.53 29,405.20 14 50,000 12,500 62,500 75,000 23,500 81,394.37 19,723.32 95,965.24 123,411.88 38,375.18 15 14,000 3,500 17,500 21,000 6,580 100,056.62 23,388.88 110,690.25 154,281.90 47,577.78 16 136,017.69 32,119.15 151,363.81 206,522.17 63,824.20 17 103,323.16 24,258.02 113,437.33 156,885.39 48,418.22 18 36,764.20 8,493.28 39,602.53 56,733.63 17,448.58 19 40,556.29 9,501.30 45,567.64 63,099.76 19,471.51 20 27,808.45 6,486.84 30,457.84 42,691.00 13,157.84 21 12,114.17 2,785.49 12,712.19 18,462.92 5,670.81 22 14,523.24 3,407.20 15,917.95 22,053.19 6,804.90 23 19,570.00 4,670.00 22,237.50 29,622.00 9,176.99 24 14,000.00 3,340.00 15,900.00 21,192.00 6,564.96 25 2,940.00 700.00 3,325.00 4,452.00 1,378.51

<tbody>
</tbody>

<tbody>
</tbody>

#### dariushou

##### Board Regular
Any help on this. My big issue is the below code. Just can't seem to figure out the right way to only have one cfArray variable instead of 5. I would think this could be done in a for loop, but i've failed miserable at trying to figure it out.

Thanks,
Darius

Code:
``````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)``````

1,082,257
Messages
5,364,074
Members
400,778
Latest member

### This Week's Hot Topics

• populate from drop list with multiple tables
Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
• Find list of words from sheet2 in sheet1 before a comma and extract text vba
Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
• Dynamic Formula entry - VBA code sought
Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...