+ Reply to Thread
Results 1 to 5 of 5

How to bullet proof my goal seek macro?

  1. #1
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    2019
    Posts
    155

    How to bullet proof my goal seek macro?

    Hi there,

    I have a pretty simple macro in my excel file that runs a few basic goal seeks. Unfortunately, this code has a tendency to crash frequently, especially when the user's inputs are too off and/or when the result is difficult or impossible to find for Excel. Usually what happens is that the macro starts running and crashes Excel. I've tried adding an "EnableCancelKey = xlErrorHandler" to give a way out for users when the macro crashes but it does not work every time.

    Please Login or Register  to view this content.
    To give some context, the macro is trying to assess the maximum purchase price based on certain targeted return indicators (COC, CF, IRR, MIRR) provided by the user.

    So my questions:
    - Is there a more "solid" approach to achieve the same result without using .GoalSeek?
    - What could I do to make that code less subject to crash? How can I better handle crashes when they occur?
    - Anything else I could do to improve the code?

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    04-16-2020
    Location
    Alberta, Canada
    MS-Off Ver
    365
    Posts
    216

    Re: How to bullet proof my goal seek macro?

    When I do a goalseek like you've done, I set the changingcell (purchase_price in your case) to 0 before I run the the rest of the code. I haven't had any problems with them crashing. Since you keep using 'purchase_price' you might want to zero it out before each goalseek.

  3. #3
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    430

    Re: How to bullet proof my goal seek macro?

    Something I have noticed too with Error Handling is that VBA will tend to catch the first error and handle it properly, but the next one it gets all tripped up. Not sure if that is the problem you are having, but it might be. You can create a function that will perform your goal seek and within that function, have your error handling. This way, each time the function gets called, the error memory is cleared. Works better for me anyways when I do that.

  4. #4
    Forum Contributor
    Join Date
    08-12-2019
    Location
    NYC
    MS-Off Ver
    2019
    Posts
    155

    Re: How to bullet proof my goal seek macro?

    Quote Originally Posted by Cobra17 View Post
    When I do a goalseek like you've done, I set the changingcell (purchase_price in your case) to 0 before I run the the rest of the code. I haven't had any problems with them crashing. Since you keep using 'purchase_price' you might want to zero it out before each goalseek.
    Thanks, that's a good idea. Might help a bit with the infinite loops. I think the main issue remains when other parameters of the model are too inconsistent for excel to return a result. In this case, the routine starts crashing.

    Quote Originally Posted by thomglea View Post
    Something I have noticed too with Error Handling is that VBA will tend to catch the first error and handle it properly, but the next one it gets all tripped up. Not sure if that is the problem you are having, but it might be. You can create a function that will perform your goal seek and within that function, have your error handling. This way, each time the function gets called, the error memory is cleared. Works better for me anyways when I do that.

    Thanks, I'm not sure to fully follow your approach. What would the macro look like then?

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: How to bullet proof my goal seek macro?

    From what I understand, Excel's built in Goal Seek uses a fairly basic Newton-Raphson or secant type of root finding algorithm. I cannot think of any universal, "magic" tricks to make the algorithm foolproof. Things that I think of to do:

    1) Do you know enough about the behavior of your functions to be able to test the quality of an initial guess before executing the goal seek?
    2) Do you know enough about the behavior of your functions to be know when there is no solution to be found before executing goal seek?
    3) Are you required to use the built in goal seek utility? NR or secant for one variable problems are not difficult to program from scratch. I have some ideas here: https://www.excelforum.com/tips-and-...ind-roots.html One advantage I find to programming my own root finding algorithms is that I have better control over the termination of the algorithm and why the algorithm terminated (beyond some kind of "goal seek crashed" termination).

    You haven't shared any of the details of your financial/mathematical model, so I cannot look at the details, but maybe something there will prompt some ideas for you to explore.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Goal Seek Macro
    By markvdhouten in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-08-2021, 06:06 PM
  2. [SOLVED] Goal Seek Macro
    By Adamsc21 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2017, 05:22 PM
  3. Goal seek in a Macro
    By Wizards in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2014, 04:04 AM
  4. Goal Seek Macro
    By worswick25 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-02-2013, 07:05 AM
  5. Goal Seek Macro?
    By caj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2012, 06:19 AM
  6. Goal Seek Macro
    By stephenp12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2011, 06:06 PM
  7. Macro help for goal seek
    By Dean in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2005, 01:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1