Export (0) Print
Expand All

Worksheet.Scenarios Method

Gets an object that represents either a single scenario (a Microsoft.Office.Interop.Excel.Scenario object) or a collection of scenarios (a Microsoft.Office.Interop.Excel.Scenarios object) on the worksheet.

Namespace: Microsoft.Office.Tools.Excel
Assembly: Microsoft.Office.Tools.Excel (in microsoft.office.tools.excel.dll)

public virtual Object Scenarios (
	[OptionalAttribute] Object Index
)

Parameters

Index

The name or number of the scenario. Use an array to specify more than one scenario.

Return Value

An object that represents either a single scenario (a Microsoft.Office.Interop.Excel.Scenario object) or a collection of scenarios (a Microsoft.Office.Interop.Excel.Scenarios object) on the worksheet.

The following code example uses the Scenarios method to add a new scenario to the current worksheet, and then displays the Scenario Manager dialog to verify that the scenario was added.

private void CreateScenario()
{   
    Excel.Range range1 = this.Range["A1", "B10"];
    range1.Formula = "=rand()";

    Excel.Scenarios scenarios1 =
        (Excel.Scenarios)this.Scenarios(missing);

    Excel.Scenario newScenario = scenarios1.Add("New Scenario", 
        range1, missing, missing, missing, missing);

    // Show Scenario Manager dialog to verify that the scenario was added.
    this.Application.Dialogs[Excel.XlBuiltInDialog.xlDialogScenarioCells].Show(
        missing, missing, missing, missing, missing, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing,
        missing, missing, missing, missing, missing, missing, missing, missing,
        missing, missing, missing, missing, missing, missing);
}

Show:
© 2014 Microsoft