Search Contextures Sites

   

Excel -- Scenarios -- Automatically Show Scenario

Create a Dropdown List of Scenarios  
Add the Event Code  
Test the Code  

 


You can use Scenarios to store several versions of the data in a worksheet. To make them easier to use, you can use programming to automatically show the selected Scenario.

This example will use the Scenarios created here:
   Scenarios -- Create and Show  

Download the zipped sample starter file

 

Create a Dropdown List of Scenarios

To view a Scenario, you can choose Tools | Scenarios, then select a Scenario, click the Show button, and close the Scenario Manager dialog box. To make it easier to switch between Scenarios, you can create a dropdown list of Scenarios, and use event code to show the selected Scenario.

Note: This example will only work in Excel 2000, or later versions.

 

 

Create a list of Scenarios

  1. On the Insert menu, choose Worksheet
  2. Name the new sheet as Lists  
  3. Starting in cell A1, type the heading, and Scenario names, as shown at right. 

 

Name the list of Scenarios

The list will be easier to maintain if it's a dynamic named range.
To create the named range:

  1. On the Lists worksheet, select cell A1
  2. Choose Insert | Name | Define
  3. For Names in Workbook, type: ScenarioList
  4. For Refers to, type the following formula:

=OFFSET(Lists!$A$1,1,0,COUNTA(Lists!$A:$A)-1,1)

 

  5. Click OK

 

Create the dropdown list

  1. On the Budget worksheet, select cell B1
  2. Choose Data | Validation
  3. From the Allow dropdown, choose List
  4. In the Source box, type:    =ScenarioList 
  5. Click OK     

Add the Event Code

  1. Right-click on the Budget sheet tab
  2. Choose View Code
  3. From the Object dropdown, at the top left, choose Worksheet.
  4. From the Procedure dropdown, at the top right, choose Change.
  5. Where the cursor is flashing, enter the following code:
  If Target.Address = "$B$1" Then
    ActiveSheet.Scenarios(Target.Value).Show
  End If
  1. Choose File | Close and Return to Microsoft Excel 

 

Test the Code

  1. On the Budget worksheet, select an item from the dropdown list in cell B1.
  2. That Scenario will be displayed.
  3. Close and save the workbook.

Note: When opening the workbook, you will have to enable macros in order to use this code.


Download the zipped sample completed file  

 

 

1. Scenarios -- Create and Show  
2. Scenarios -- Scenario Summaries
  
3. Scenarios -- Automatically Show Scenario  
4. Scenarios -- Programming  

 

 
   
       Home     Excel Tips     Excel Files      Blog    Contact

RSS Feed

Privacy Policy

 

 

The Excel Store

 

Last updated: February 26, 2009 1:18 AM