NOTE: this page describes running simulations from VBA in RiskAMP version 6. VBA support has changed from prior versions. For information on scripting simulations in RiskAMP version 4 or 5, see this page.
The basic function for running a simulation from VBA looks like this:
Application.Run "RiskAMP.RunSimulation", Trials, Echo, Block
The function takes three arguments:
Trials | The number of trials to run. If you omit this argument, the command will open the simulation dialog but won't start running. |
Echo | Whether to show screen updates. Set to True to show screen updates while running. |
Block | Whether VBA should wait for the simulation to complete. Set to True if you want your script to block; this is useful if you want to run code that uses the results of the simulation. |
Here’s a complete example:
'
' this subrutine will run 1000 iterations. it will block, so the
' message box will show up after the simulation is complete.
'
Sub RunSimulation()
Application.Run "RiskAMP.RunSimulation", 1000, False, True
MsgBox "Simulation complete"
End Sub
Note that screen updates and blocking don’t work together. If you are blocking VBA, that will have the effect of blocking screen updates as well.