Home → Techniques and Tips → VBA Programming with @RISK → Accessing Simulation Data in VBA Code
Applies to: @RISK 5.x–7.x
Can I access @RISK worksheet functions in Visual Basic?
To call @RISK functions from Visual Basic, you must set up a reference from Visual Basic Editor to @RISK via Tools » References in the editor. Please see Setting References in Visual Basic for the appropriate reference and how to set it.
Please see the XDK or Developer Kit manual for details on the methods mentioned here, as well as alternative methods. (Beginning with @RISK 6.2, start with the Automation Guide for a high-level introduction: Help » Developer Kit (XDK) » Automation Guide.)
How can I retrieve simulation data, in a way similar to the RiskData( ) worksheet function?
Use the GetSampleData method to fill an array with the simulated data. Here's an example:
numSamples = _
Risk.Simulation.Results.GetSimulatedOutput("MyOutput"). _
GetSampleData(sampleData, True)
This fills the VBA array sampleData with all the data from the named output, and returns the number of samples. (Although this example shows getting data from an output, you can also use GetSampleData with GetSimulatedInput.)
How can I get the statistics of a simulated input or output, such as a simulated mean or percentile?
Use Mean, Percentile, or a similar property of the RiskSimulatedResult object. Here's an example:
MsgBox "The mean of MyOutput is " & _
Risk.Simulation.Results.GetSimulatedOutput("MyOutput").Mean
(Again, you could also use this technique with GetSimulatedInput to get statistics of a simulated input.)
See also: Sampling @RISK Distributions in VBA Code to get random numbers from an @RISK distribution without running a simulation.
Last edited: 2018-02-28