This documentation is archived and is not being maintained.

NamedRange.GoalSeek Method

Calculates the values necessary to achieve a specific goal.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

bool GoalSeek(
	Object Goal,
	Range ChangingCell
)

Parameters

Goal
Type: System.Object

The value you want returned in this cell.

ChangingCell
Type: Microsoft.Office.Interop.Excel.Range

Specifies which cell should be changed to achieve the target value.

Return Value

Type: System.Boolean
true if the goal seek is successful; otherwise, false.

If the goal is an amount returned by a formula, this calculates a value that, when supplied to your formula, causes the formula to return the number you want.

The following code example sets a formula in a NamedRange control, and then uses the GoalSeek method to determine which value for X in the formula will return the value 15. The example places the answer in cell B1.

This example is for a document-level customization.

private void FindGoal()
{
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1"],
        "namedRange1");

    this.Range["B1"].Name = "X";
    namedRange1.Formula = "=(X^3)+(3*X^2)+6";
    namedRange1.GoalSeek(15, this.Range["B1"]);
}

Show: