Partilhar via


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)

Syntax

'Declaration
Function GoalSeek ( _
    Goal As Object, _
    ChangingCell As Range _
) As Boolean
bool GoalSeek(
    Object Goal,
    Range ChangingCell
)

Parameters

  • Goal
    Type: System.Object

    The value you want returned in this cell.

Return Value

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

Remarks

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.

Examples

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 Sub FindGoal()
        Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange _
            = Me.Controls.AddNamedRange(Me.Range("A1"), _
            "namedRange1")

        Me.Range("B1").Name = "X"
        namedRange1.Formula = "=(X^3)+(3*X^2)+6"
        namedRange1.GoalSeek(15, Me.Range("B1"))
    End Sub
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"]);
}

.NET Framework Security

See Also

Reference

NamedRange Interface

Microsoft.Office.Tools.Excel Namespace