No results found

    We couldn’t find anything with that term. Please try again.

    Press Esc to close

    VBA

    Support for Running Simulations from Macros or Scripts


    The basic function for running a simulation from VBA looks like this:

    Application.Run "RiskAMP.RunSimulation", Trials, Echo, Block 

    The function takes three arguments:

    TrialsThe number of trials to run. If you omit this argument, the command will open the simulation dialog but won’t start running.
    EchoWhether to show screen updates. Set to True to show screen updates while running.
    BlockWhether VBA should wait for the simulation to complete. See below for an explanation of blocking.

    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

    About blocking

    If the Block parameter is set to False, the function will return immediately after starting a simulation. If all you are doing is running a simulation from a button in the spreadsheet, this is probably fine.

    If however you want your script to analyze the results of the simulation, you probably want to block execution until the simulation is complete.

    That’s what the Block parameter does. It tells VBA to wait until the simulation is complete before running the next line of code. In the example above, because it’s set to block, the message box will not show up until the simulation has finished.

    If you set Block to False in that example, though, the message box will pop up immediately after the simulation starts.

    Blocking and screen updates

    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.