This repo contains the VBA
code for a way to implement scenario analysis in Excel using a VBA
macro. This method requires no changes to your model sheets. The scenarios are generated by adding two sheets to a worbook to house the scenario definitions and the scenario results.
Excel offers a built-in scenario function which allows a user to define multiple scenarios (a set of input cells and values) and then view their model with any of those scenarios without having to manually change each of the input cells. These scenarios are manually defined one at a time. This built-in methodology is good for a very small number of scenarios as it is time consuming to enter and manage.
I was looking for a way to automate the generation of scenarios (in some cases >200 scenarios) and more importantly, the recording of the scenario results into a table that can easily be summarized and manipulated in excel.
That is the purpose of this macro. Please note that this macro is completely stand-alone and does not interact at all with the scenario tool built into excel.
The VBA
code is stored in the scenarios.bas
file in the repo, which represents the contents of one VBA
module. It essentially follows the following steps:
- Record the baseline input values that are in the model prior to generating the scenarios
- Loop through each of the input variables (ten slots available)
- Further loop through the values defined for each of the variables in the from and to columns (defined using the following fields: from, to and increment)
- Input the values for each of the variables
- Record the output cell values and paste them in the results table
- Restore the model the original baseline input values that were in the model prior to generating the scenarios
Let's take the example provided in the Example.xlsm
workbook. Assume you have built a model - it can be as complicated as you need it to be, i.e. 10, 15 sheets, whatever is neccessary. The model has numerous assumptions (10,20,30 etc.) and multiple output values (such as profit by year for a multiple year period).
Once the structure of the model is complete you want to understand the relationship between the various assumption inputs and the model outputs. How do they move together? Which inputs are the outputs most sensitive to? Etc.
The problem is that the input assumptions and outputs are typically scattered across multiple worksheets in a typically relatively complicated model. The tool that most people turn to for scenario analysis are Data Tables
which are a built-in excel function. Data Tables
, however, have multiple limitations. For one, the assumption inputs must be located on the same sheet as the Data Table
which is extremely limited for a typical complex multi-sheet model. The second limitation is that a Data Table
can only show a scenario where two variables are changed simultaneously. In a normal complex model there are often situations where changing multiple assumptions at the same time is a very resonable thing to do.
Using the scenarios
macro in this repo the above limitations can be solved with a relativley easy and powerful solution.
The macro uses two custom worksheets which can be found in the example.xlsm
file. The first is the s_def
worksheet which is where the scenarios are to be defined and is displayed below. Simply list the input assumptions in the table as seen below, telling the macro the sheet
and cell
location of each input. The assumptions can reside across any number of worksheets - no limitation. Then fill out the values that you want each input assumption to take using the From, To and Increment field. For example, the macro will take the price per unit cell in the following location: model!B4
and will enter in the following values: 9
, 10
, 11
, 12
, and 13
. In fact, every combination listed in the table below, which works out to be 225
combinations will be entered into the spreadsheet and the values found in the output cells, as defined in the table on the right below, will be recorded and stored in the s_res
sheet. You can see how quickly the number of scenarios can multiple when changing only 4 assumption inputs and varying them over a tight range of values. Trying to handle that with the built-in scenario tool in Excel is impossible and this just cannot be accomplished with only two-dimensional Data Tables
.
The s_res
sheet which houses the scenario results is simply set up as a table. The left half of the table records the values of the assumption inputs that define the scenario and the right half records the values of the output cells. This is stored in an easy to use table format. This table of scenario results can then be used to fill a pivot table or pivot chart to analyze how changes to inputs cause changes in model outputs. This is a very powerful way to understand how a complicated model works when inputs are changed.
See the .xlsm
macro-enabled file in the repo for an example implementation of this macro. There are many use cases, but the file shows one simple model sheet, a scenario definition sheet which defines input variables, and a scenario results sheet which houses the results of 225 scenarios that have been generated from the macro.
By way of example, I have included a pivot table sheet showing how you can simply manipulate the scenario results data to easily see the relationship between multiple inputs and multiple outputs.
In order to implement this macro you must copy the s_def
and s_res
sheets from the sample excel file and put them into your workbook in the same format. After the sheets are in your workbook simply fill out the s_def
sheet to include the location and value ranges for the input cells and definte the location of the output cells that you want to watch. Then simply run the macro called RunScenarios
from the Tools -> Macro menu.
Please note that I have not spent any time on formatting the Excel file. Formatting is a personal preference and I chose to spend time on functionality over format.
To install the custom function you can simply copy the contents of the VBA
module in this repo, go to Excel > Tools > Macros > Visual Basic Editor. Then insert a new module and paste the code. You can then return to your excel worbook and insert the formula into a cell as if it were a built-in formula.
###Additional VBA functions
This macro makes use of a modified version of the =befe()
custom VBA
function that I have created. These formulas are on the top of the s_res
sheet and make like a lot easier when trying to copy out the scenario data without having to traverse multiple worksheets to find each cell. You might want to take a look at that repo as well if you are interested in how that custom function works. I have found it to be a significant time saver in other instances as well.