13
The arguments of GetSimulatedOutput specify the output you want. The first argument is either the
name of the output, implicitly or explicitly defined in the RiskOutput function, or a cell reference. (It can
also be an integer, such as 3, meaning the third output, but this isn’t usually a good practice unless you
are sure of the ordering of the outputs.) An optional second argument when there are multiple
simulations is the index of the simulation. The arguments of GetSimulatedInput are similar, except that
if a name is used for the first argument, it must be the name specified in the RiskName property
function. For example, if the formula in an input cell is =RiskNormal(1000,100,RiskName(“Q1_Cost”)),
then the name “Q1_Cost” can be referenced.
Note that if you ask for the results of a simulation that has not yet been run, you will get an error. You
can check for this with the following line:
If Not Risk.Simulation.Results.Exist Then Risk.Simulation.Start
Alternatively, if this condition is true (no simulation results exist), you could display an error message,
informing the user that a simulation needs to be run, and then quit prematurely. The following code
illustrates what this might look like.
Sub RunWithErrorCheck()
If Not Risk.Simulation.Results.Exist Then
MsgBox "You need to run a simulation first.", vbInformation
Exit Sub
Else
Range("B20").Value = Risk.Simulation.Results.GetSimulatedOutput("Profit").Mean
End If
End Sub
Displaying Graphical Results of a Simulation
You can use the RiskGraph object to create a variety of graphs. Starting with Risk.Simulation.Results,
you can use a number of functions that return a RiskGraph object: GraphDistribution, GraphScatter,
GraphSensitivityTornado, etc. Because these functions return different types of graphs, their arguments
vary slightly, as will be explained shortly. However, you typically use the following three steps with all of
them.
1. Set a reference to a graph.
Dim rGraph As RiskGraph
Set rGraph = Risk.Simulation.Results.GraphDistribution(“Profit”)
2. Change one or more properties of the graph.
rGraph.TitleMainText = “Distribution of Profit”
3. Send the image of the graph to a worksheet, where the last four arguments determine the size and
position of the graph. (You can also send the image to the clipboard or to a file with the
ImageToClipboard or ImageToFile functions.)
rGraph.ImageToWorksheet ActiveSheet, RiskImageFormat_BMP, 100, 100, 300, 250
Equivalently, you can do all of these steps with the following With block, without requiring the rGraph
variable. Note that the first line creates an implicit RiskGraph object, so that you can refer to its
TitleMainText property and its ImageToWorksheet method in the next two lines.
With Risk.Simulation.Results.GraphDistribution(“Profit”)
.TitleMainText = “Distribution of Profit”
.ImageToWorksheet ActiveSheet, RiskImageFormat_BMP, 100, 100, 300, 250
End With