Speeding up simulations with arrays

John Lynam

New Member
Joined
Jan 26, 2017
Messages
2
Hi all,

I was wondering if anyone can help me.

I have a workbook that does some relatively simple financial modelling with one stochastic variable - annual equity returns (the model assumes they are normally distributed).

There are three outputs I want to capture for each simulation - values in cells H2, H3 and H4.

I currently run a Monte Carlo simulation on the model and copy the outputs onto a new page, one below the other (they get transposed when copied).

At the moment my code refreshes the calculation, then simply copies and pastes values and then repeats the process. It is a simple for i = 1 to 10,000 loop. Screen updating is off etc. It works fine but I would like to speed it up.

Here is my question. Is it more efficient to collect the outputs (H2, H3, H4 - perhaps transposed if necessary but I don't know how to do that in an array) in a VBA array for the 10,000 simulations and then spit them out onto the new sheet, or is what I am doing currently just as fast. I am guessing arrays can speed things up.

If anyone has any code lying around that could capture three pieces of data, over and over, in an array and then spit it out in three columns on a new sheet that would be great.

Any help would be much appreciated.

Cheers
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Generally speaking, accessing the document less often will probably yield speed improvements although probably not massively unless your sheet recalculates a lot during the generation process and is otherwise complex. Only one way to find out for sure though...
 
Upvote 0
Below I give an example of a subroutine in which a matrix with 1000 rows and 3 rows is created.
Next, its contents is 'dumped' in a specified range.
I think this can inspire you to work out a solution for your application.
Succes !

Sub test()
Dim j As Integer
Dim D(1000, 3) As Single
For j = 1 To 1000
D(j, 1) = j: D(j, 2) = j ^ 2: D(j, 3) = j ^ 3
Next j
Worksheets(1).Range("A1:C1000") = D
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top