Hello all. Here I am again in the need of your help.
I have a VBA that I use to set values in my excel model. Each of those values are linked to a cash-flow schedule that I use to calculate IRR (using XIRR). The same VBA that set the values of the cash-flows also writes the resulting IRR in one of the worksheets in my model.
There are 15 cash-flows, and for each of them, I have 3 possible values (let's say 3 cases).
I need to calculate the resulting IRR for all the possible combination of cash-flows and scenarios. (3^15 = 14,348,907 possible arranges).
The way I managed to do it was build 15 For loops, one inside each other. It worked, but it took me 4 days to run it.
I would like some advice on how to improve it and make it run faster - and possibly a way to make the number of for loops dynamic, or leaner.
Any help is welcome.
Here is my code:
Sub MC()
Dim Q_Val As Range
Dim VA, VB, VC, VD, VE, VF, VG, VH, VI, VJ, VK, VL, VM, VN, VO, r, n, Total As Long
Dim ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn, co, m As Integer
Dim pctCompl, IRR As Single
Set Q_Val = Range("N_Val") ' number of valuations (counter for the for loop)
ca = 1
cb = 1
cc = 1
cd = 1
ce = 1
cf = 1
cg = 1
ch = 1
ci = 1
cj = 1
ck = 1
cl = 1
cm = 1
cn = 1
co = 1
r = 1 'row to write
n = 1 'total loop count
m = 1 'collumn to write
Application.ScreenUpdating = False
Total = Worksheets("Summary").Cells(5, 3).Value
Worksheets("Results").Activate
Cells.Select
Selection.delete Shift:=xlUp 'clears up whatever values are writen in the Results worksheet
For ca = 1 To Q_Val 'Asset 1
VA = Worksheets("Cases").Cells(ca, 1).Value
For cb = 1 To Q_Val 'Asset 2
VB = Worksheets("Cases").Cells(cb, 2).Value
For cc = 1 To Q_Val 'Asset 3
VC = Worksheets("Cases").Cells(cc, 3).Value
For cd = 1 To Q_Val 'Asset 4
VD = Worksheets("Cases").Cells(cd, 4).Value
For ce = 1 To Q_Val 'Asset 5
VE = Worksheets("Cases").Cells(ce, 5).Value
For cf = 1 To Q_Val 'Asset 6
VF = Worksheets("Cases").Cells(cf, 6).Value
For cg = 1 To Q_Val 'Asset 7
VG = Worksheets("Cases").Cells(cg, 7).Value
For ch = 1 To Q_Val 'Asset 8
VH = Worksheets("Cases").Cells(ch, 8).Value
For ci = 1 To Q_Val 'Asset 9
VI = Worksheets("Cases").Cells(ci, 9).Value
For cj = 1 To Q_Val 'Asset 10
VJ = Worksheets("Cases").Cells(cj, 10).Value
For ck = 1 To Q_Val 'Asset 11
VK = Worksheets("Cases").Cells(ck, 11).Value
For cl = 1 To Q_Val 'Asset 12
VL = Worksheets("Cases").Cells(cl, 12).Value
For cm = 1 To Q_Val 'Asset 13
VM = Worksheets("Cases").Cells(cm, 13).Value
For cn = 1 To Q_Val 'Asset 14
VN = Worksheets("Cases").Cells(cn, 14).Value
For co = 1 To Q_Val 'Asset 15
VO = Worksheets("Cases").Cells(co, 15).Value
Worksheets("Transfer").Cells(1, 2).Value = VA
Worksheets("Transfer").Cells(2, 2).Value = VB
Worksheets("Transfer").Cells(3, 2).Value = VC
Worksheets("Transfer").Cells(4, 2).Value = VD
Worksheets("Transfer").Cells(5, 2).Value = VE
Worksheets("Transfer").Cells(6, 2).Value = VF
Worksheets("Transfer").Cells(7, 2).Value = VG
Worksheets("Transfer").Cells(8, 2).Value = VH
Worksheets("Transfer").Cells(9, 2).Value = VI
Worksheets("Transfer").Cells(10, 2).Value = VJ
Worksheets("Transfer").Cells(11, 2).Value = VK
Worksheets("Transfer").Cells(12, 2).Value = VL
Worksheets("Transfer").Cells(13, 2).Value = VM
Worksheets("Transfer").Cells(14, 2).Value = VN
Worksheets("Transfer").Cells(15, 2).Value = VO
IRR = Round(Worksheets("Transfer").Cells(17, 2).Value, 4)
If r > 1048576 Then
r = r - 1048576
m = m + 1
'1048576 rows tops
End If
Worksheets("Results").Cells(r, m).Value = IRR
progress pctCompl
n = n + 1
r = r + 1
Next co
Next cn
Next cm
Next cl
Next ck
Next cj
Next ci
Next ch
Next cg
Next cf
Next ce
Next cd
Next cc
Next cb
Next ca
Application.ScreenUpdating = True
End Sub
I have a VBA that I use to set values in my excel model. Each of those values are linked to a cash-flow schedule that I use to calculate IRR (using XIRR). The same VBA that set the values of the cash-flows also writes the resulting IRR in one of the worksheets in my model.
There are 15 cash-flows, and for each of them, I have 3 possible values (let's say 3 cases).
I need to calculate the resulting IRR for all the possible combination of cash-flows and scenarios. (3^15 = 14,348,907 possible arranges).
The way I managed to do it was build 15 For loops, one inside each other. It worked, but it took me 4 days to run it.
I would like some advice on how to improve it and make it run faster - and possibly a way to make the number of for loops dynamic, or leaner.
Any help is welcome.
Here is my code:
Sub MC()
Dim Q_Val As Range
Dim VA, VB, VC, VD, VE, VF, VG, VH, VI, VJ, VK, VL, VM, VN, VO, r, n, Total As Long
Dim ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn, co, m As Integer
Dim pctCompl, IRR As Single
Set Q_Val = Range("N_Val") ' number of valuations (counter for the for loop)
ca = 1
cb = 1
cc = 1
cd = 1
ce = 1
cf = 1
cg = 1
ch = 1
ci = 1
cj = 1
ck = 1
cl = 1
cm = 1
cn = 1
co = 1
r = 1 'row to write
n = 1 'total loop count
m = 1 'collumn to write
Application.ScreenUpdating = False
Total = Worksheets("Summary").Cells(5, 3).Value
Worksheets("Results").Activate
Cells.Select
Selection.delete Shift:=xlUp 'clears up whatever values are writen in the Results worksheet
For ca = 1 To Q_Val 'Asset 1
VA = Worksheets("Cases").Cells(ca, 1).Value
For cb = 1 To Q_Val 'Asset 2
VB = Worksheets("Cases").Cells(cb, 2).Value
For cc = 1 To Q_Val 'Asset 3
VC = Worksheets("Cases").Cells(cc, 3).Value
For cd = 1 To Q_Val 'Asset 4
VD = Worksheets("Cases").Cells(cd, 4).Value
For ce = 1 To Q_Val 'Asset 5
VE = Worksheets("Cases").Cells(ce, 5).Value
For cf = 1 To Q_Val 'Asset 6
VF = Worksheets("Cases").Cells(cf, 6).Value
For cg = 1 To Q_Val 'Asset 7
VG = Worksheets("Cases").Cells(cg, 7).Value
For ch = 1 To Q_Val 'Asset 8
VH = Worksheets("Cases").Cells(ch, 8).Value
For ci = 1 To Q_Val 'Asset 9
VI = Worksheets("Cases").Cells(ci, 9).Value
For cj = 1 To Q_Val 'Asset 10
VJ = Worksheets("Cases").Cells(cj, 10).Value
For ck = 1 To Q_Val 'Asset 11
VK = Worksheets("Cases").Cells(ck, 11).Value
For cl = 1 To Q_Val 'Asset 12
VL = Worksheets("Cases").Cells(cl, 12).Value
For cm = 1 To Q_Val 'Asset 13
VM = Worksheets("Cases").Cells(cm, 13).Value
For cn = 1 To Q_Val 'Asset 14
VN = Worksheets("Cases").Cells(cn, 14).Value
For co = 1 To Q_Val 'Asset 15
VO = Worksheets("Cases").Cells(co, 15).Value
Worksheets("Transfer").Cells(1, 2).Value = VA
Worksheets("Transfer").Cells(2, 2).Value = VB
Worksheets("Transfer").Cells(3, 2).Value = VC
Worksheets("Transfer").Cells(4, 2).Value = VD
Worksheets("Transfer").Cells(5, 2).Value = VE
Worksheets("Transfer").Cells(6, 2).Value = VF
Worksheets("Transfer").Cells(7, 2).Value = VG
Worksheets("Transfer").Cells(8, 2).Value = VH
Worksheets("Transfer").Cells(9, 2).Value = VI
Worksheets("Transfer").Cells(10, 2).Value = VJ
Worksheets("Transfer").Cells(11, 2).Value = VK
Worksheets("Transfer").Cells(12, 2).Value = VL
Worksheets("Transfer").Cells(13, 2).Value = VM
Worksheets("Transfer").Cells(14, 2).Value = VN
Worksheets("Transfer").Cells(15, 2).Value = VO
IRR = Round(Worksheets("Transfer").Cells(17, 2).Value, 4)
If r > 1048576 Then
r = r - 1048576
m = m + 1
'1048576 rows tops
End If
Worksheets("Results").Cells(r, m).Value = IRR
progress pctCompl
n = n + 1
r = r + 1
Next co
Next cn
Next cm
Next cl
Next ck
Next cj
Next ci
Next ch
Next cg
Next cf
Next ce
Next cd
Next cc
Next cb
Next ca
Application.ScreenUpdating = True
End Sub