Running User-Defined Macros

You can run user-defined Microsoft Excel VBA macros automatically during a simulation by naming them with predefined names:

The following figure (Figure 21, When User-Defined Macros Run) shows where each macro fits into the Crystal Ball simulation cycle.

Figure 21. When User-Defined Macros Run

This diagram illustrates the sequence of defined macros within the Crystal Ball simulation cycle: From Start (CB Before Simulation and CB Before Trial), to Set Values, to Recalculate (CD After Recalc), to Get Results (CB After Trial), to Stop (CB After Simulation) and End.

Crystal Ball searches for macros with these names in open workbooks and runs them at the appropriate points during the simulation. The macros must be contained within workbooks and worksheets (not VBA modules) and Run User-Defined Macros must checked on the Options tab of the Run Preferences dialog.

Crystal Ball checks for the presence of any CBBeforeTrial, CBAfterTrial, and CBAfterRecalc macros before the simulation runs by attempting to execute them. If this poses a problem, you may skip the logic in the macro by first testing for aTrial = 0.

In Extreme speed, only two user-defined macros are allowed: CBBeforeSimulation and CBAfterSimulation. For more information on running user-defined macros in Extreme speed, see Running User-Defined Macros.

The following sections provide details on user-defined macros: