cellMatrix.net
Spreadsheet Modeling and Related Topics

About

Formulas, Charts, and Models Created with Microsoft Excel.
More . . .

Statistics

  • Total Entries - 102
  • Current Viewers - 14

Categories

Recent Comments

Syndicate

Validate

Simple Goal Seek Procedure

Unfortunately Excel's goal seeking feature forces you to hand-enter a value into the "To value" field of the input box. You can't directly tie this input to a cell like you can with the "Set cell" and "By changing cell" inputs (see the screenshot below).

image

This can make things difficult especially if you're trying to seek very large values or values that that are carried out to many decimal points.

The following procedure allows you to get around this limitation. First, enter the cell addresses for the variables called Rng1, Rng2, and Rng3. Note that a formula must appear in Rng1 and a value must be entered into Rng3. Next, run the procedure.

Sub GoalSeek()

    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim Rng3 As Range

    On Error GoTo errHandler:

    'Set the value in this cell
    Set Rng1 = ActiveSheet.Range("C4")

    'Equal to the value in this cell
    'Note:  This cell must contain a formula
    Set Rng2 = ActiveSheet.Range("C7")

    'By changing the value in this cell
    Set Rng3 = ActiveSheet.Range("C2")

    If Rng1.HasFormula = True Then
        If IsNumeric(Rng3) = True Then
            Rng1.GoalSeek Goal:=Rng2, ChangingCell:=Rng3
        End If
    End If

    Exit Sub

errHandler:
    MsgBox "Procedure failed - please check inputs."

End Sub

Another approach is to adjust your Set Cell so it is a formula that subtracts the actual value from the target value, and set your To Value to zero. It’s easy enough to do this manually, and someone smarter than I told me these algorithms are better when seeking a value of zero rather than some arbitrary nonzero value.

Posted by Jon Peltier  on  04/06  at  07:40 AM


Comment Form

Name: (Required)
E-Mail Address: (Optional)
Location: (Optional)
Web Site Address: (Optional)

Remember my personal information?
Notify me of follow-up comments?

Before submitting your comment, please enter the phrase you see below: