XL: GoalSeek Method in VB Macro Returns the Wrong Answer

ID: Q124096

The information in this article applies to:

SYMPTOMS

The GoalSeek method in Visual Basic for Applications may return an answer that is different from the one returned by the Goal Seek command on the Tools menu. The correct answer is the one returned when you choose Goal Seek from the Tools menu.

WORKAROUND

If the GoalSeek method does not return the correct answer, use the Application.ExecuteExcel4Macro command to run the Microsoft Excel 4.0 GOAL.SEEK macro command.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/ 

The following example sets cell A1 to the value 15 by changing cell B1.

Example

1. Type the following into a worksheet:

      A1: =(B1^3)+(3*B1^2)+6   B1: (blank)

2. In a module sheet, type the following:

      Sub Test()
         Application.ExecuteExcel4Macro "GOAL.SEEK(""r1c1"",15,""r1c2"")"
      End Sub

   If you want the second parameter (the "To Value" parameter) to refer to
   a specific cell (D1, for example) on the worksheet, rather than a fixed
   value, use the following syntax:

         Application.ExecuteExcel4Macro "GOAL.SEEK(""r1c1""," & _
            Range("D1").Value & ",""r1c2"")"

This example assumes that the number 15 has been entered in cell D1.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

REFERENCES

"User's Guide," version 5.0, Chapter 6, "Seeking a Value That Solves a Formula"

For more information about the GoalSeek Method, choose the Search button in Visual Basic Reference Help, and type the following:

   GoalSeek

Additional query words: 5.00 5.00c 7.00
Keywords          : kbcode kbprg kbtool 
Version           : WINDOWS:5.0,5.0c,7.0; MACINTOSH:5.0
Platform          : MACINTOSH WINDOWS
Issue type        : kbbug

Last Reviewed: May 17, 1999