You can run user-defined Microsoft Excel VBA macros automatically during a simulation by naming them with predefined names:
CBBeforeSimulation — Runs immediately after the Start Simulation command or Single-Step command
CBBeforeTrial — Runs before random numbers have been placed in assumption cells
CBAfterRecalc — Runs after Microsoft Excel has recalculated the model but before a trial value has been retrieved from the forecast cells
CBAfterTrial — Runs after the forecast trial values have been retrieved and entered into the forecast charts
CBAfterSimulation — Runs when the simulation is complete or stopped with a Stop command, by single-stepping (after each step), or for any other reason
The following figure (Figure 21, When User-Defined Macros Run) shows where each macro fits into the Crystal Ball simulation cycle.
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: