Monte Carlo Macro - Optimized for Speed

LactoseO.D.'d

Board Regular
Joined
Feb 22, 2010
Messages
52
I am trying to create a Monte Carlo simulator from scratch with the computational speed of Crystal Ball or better. Currently, the solutions I've been able to come up with take something like 20 seconds to run a simple 500 trials by varying values within the a spreadsheet.

I am thinking that if I can write the macro without referencing cells until the very end, it will run faster. Is there a way to generate a list of randomized trial results given distribution types, means, and stdevs and then populate it to a range of cells at the very end? Would this speed things up? I am looking to get in to the 10-20k trials range and things must go faster. I have seen it done, I just don't know how :(
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You could do all of your calculations using arrays and then write the array at the end of the process. That would normally be faster than reading/writing to cells. Simple example:

Code:
Sub UsingArrays()

    Dim arrNums(99, 0) As Double
    Dim lIndex As Long

    For lIndex = LBound(arrNums) To UBound(arrNums)

        arrNums(lIndex, 0) = Rnd

    Next lIndex

    Range("A1:A100") = arrNums

End Sub

HTH
DK
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

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