IntroductionUse the macros on this page to automate your work with Excel Scenarios. For example,
All of the examples on this page are based on Department Budget Scenarios from the Macro to Show Excel Scenario page. In that tutorial, you saw how to automate Scenario selection. Just choose a Scenario name from a drop down list, and a macro automatically shows the values from that Scenareio Get a List of Excel ScenariosTo list all the Scenarios on a worksheet, you can adapt the following macro. This example lists all the Scenarios from the Budget worksheet, starting the list in cell A2 on the Lists worksheet. It then sorts the list alphabetically. Note: This list is used as a source for the drop down list of Department, on the Budget sheet Sub ScenarioList() ''used for Budget Sheet drop down Dim sc As Scenario Dim wsBudget As Worksheet Dim wsLists As Worksheet Dim iRow As Integer iRow = 2 'leave row 1 for heading Set wsBudget = Worksheets("Budget") Set wsLists = Worksheets("Lists") wsLists.Columns(1).ClearContents wsLists.Cells(1, 1).Value = "Scenarios" For Each sc In wsBudget.Scenarios wsLists.Cells(iRow, 1).Value = sc.Name iRow = iRow + 1 Next sc With wsLists .Range(.Cells(1, 1), .Cells(iRow - 1, 1)).Sort _ Key1:=.Cells(1, 1), _ Order1:=xlAscending, Header:=xlYes End With End Sub |
Add Error Handling to the Event CodeThe Budget worksheet has event code that displays a Scenario when a Scenario name is selected in cell B1. The following revised code will handle errors if a new Scenario name is entered in cell B1.
Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo errHandler Application.EnableEvents = False If Target.Address = Range("Dept").Address Then ActiveSheet.Scenarios(Target.Value).Show End If ExitHandler: Application.EnableEvents = True Exit Sub errHandler: If Err.Number = 1004 Then MsgBox "That Scenario is not available" Else MsgBox Err.Number & ": " & Err.Description End If Resume ExitHandler End Sub
|
Add the CreateScenario codeTo create a new Scenario on a worksheet, you can adapt the following code. This example creates a Scenario from the data entered on the Budget worksheet, and uses the department name as the Scenario name. Sub CreateScenario() On Error GoTo errHandler Dim wsBudget As Worksheet Dim wsLists As Worksheet Dim rngChange As Range Dim strName As String Dim sc As Scenario Dim lRow As Integer Set wsBudget = Worksheets("Budget") Set wsLists = Worksheets("ListsOn Error GoTo errHandler Dim wsBudget As Worksheet Dim wsLists As Worksheet Dim rngChange As Range Dim strName As String Dim sc As Scenario Dim lRow As Integer Set wsBudget = Worksheets("Budget") Set wsLists = Worksheets("Lists") Set rngChange = wsBudget.Range("Dept,Sales,Expenses") strName = wsBudget.Range("Dept").Value 'create the scenario wsBudget.Scenarios.Add Name:=strName, _ ChangingCells:=rngChange 'add to Scenario list ''used for drop down ScenarioList 'add to Value Settings list ScenarioListSet Exit Sub errHandler: If Err.Number = 1004 Then MsgBox "The Scenario could not be created." _ & vbCrLf & "Please try a different name." Else MsgBox Err.Number & ": " & Err.Description End If End Sub |
Update Scenarios from WorksheetUsually, to make changes to the values in a Scenario, you have to open the Scenario Manager, and go through a few screen, to get to the Scenario Values dialog box. For example, here is the screen where you can edit the Scenario values for the Marketing department's budget data. In a workbook with multiple Scenarios, it could take a long time to edit each one manually, going through a series of steps. Worksheet Settings ListTo make it easier to update Scenarios, the Excel sample file, that you can download at the end of this page, has a value settings list on a worksheet, where you can quickly view and edit the data for each Scenario.
In the settings list shown in the screen shot shown:
Note: For your own Scenario workbooks, you could revise the list, and the macro code, to include more value columns. Update Scenario ValuesAfter you make changes to any of the Scenario value settings, click the macro button on the Settings worksheet. That runs a macro named ScenarioUpdate (shown below), to update each Scenario with the data from the Settings list on the worksheet. |
Here is the code for the ScenarioUpdate macro. The code is stored on a regular code module, named modSettings. Sub ScenarioUpdate() 'update Scenarios from values ' on Settings sheet On Error GoTo errHandler Dim wsBgt As Worksheet Dim wsSet As Worksheet Dim rngVal As Range Dim rngChg As Range Dim c As Range Dim cVal As String Dim myDept As String Set wsBgt = Worksheets("Budget") Set wsSet = Worksheets("Settings") Set rngVal = wsSet.Range("ValueSet") Set rngChg = wsBgt.Range("Dept,Sales,Expenses") myDept = wsBgt.Range("Dept").Value For Each c In rngVal.Columns(1).Cells cVal = c.Value wsBgt.Scenarios(c.Value).ChangeScenario _ ChangingCells:=rngChg, _ Values:=Array(c.Value, _ c.Offset(0, 1).Value, _ c.Offset(0, 2).Value) Next c wsBgt.Scenarios(myDept).Show Exit Sub errHandler: If Err.Number = 1004 Then MsgBox "The " & cVal _ & "Scenario could not be updated." Else MsgBox Err.Number & ": " & Err.Description End If End Sub |
Add New Scenarios to Settings ListThe sample workbook has another macro, named ScenarioListSet, which runs automatically, when you use the Create Scenario button. Near the end of the CreateScenario macro, the following line runs the ScenarioListSet macro 'add to Value Settings list ScenarioListSet The ScenarioListSet macro (code shown below) adds the new scenario, and its values, to the Scenario Value Settings list, and sorts the Scenario names, A to Z. Here is the macro code, which is stored on the regular code module, named modSettings Sub ScenarioListSet() ''add new Scenario to Settings list Dim wsBgt As Worksheet Dim wsSet As Worksheet Dim rngVal As Range Dim rngChg As Range Dim myRow As Long Dim myCol As Long Dim lastRow As Range Set wsBgt = Worksheets("Budget") Set wsSet = Worksheets("Settings") Set rngVal = wsSet.Range("ValueSet") Set rngChg = wsBgt.Range("Dept,Sales,Expenses") Set lastRow = rngVal.Rows(rngVal.Rows.Count) myRow = lastRow.Row myCol = rngVal.Columns(1).Column rngChg.Copy wsSet.Cells(myRow + 1, myCol).PasteSpecial _ Paste:=xlPasteValues, Transpose:=True lastRow.Copy wsSet.Cells(myRow + 1, myCol).PasteSpecial _ Paste:=xlPasteFormats 'reset the range Set rngVal = wsSet.Range("ValueSet") rngVal.Sort _ Key1:=rngVal.Cells(1, 1), _ Order1:=xlAscending, _ Header:=xlNo End Sub |
Download the Sample FileClick this link to get the completed Excel Scenarios Macros workbook. The zipped file is in xlsm format, and contains macros. Be sure to enable macros when you open the workbook, if you want to test the code. More Scenario Tutorials |
Last updated: August 12, 2022 10:03 AM