+ Reply to Thread
Results 1 to 10 of 10

Finding solution by narrowing down

  1. #1
    Registered User
    Join Date
    12-20-2010
    Location
    the Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    5

    Post Finding solution by narrowing down

    Hi guys,

    I am new to this forum and obviously have an Excel VBA question:

    I have a macro that finds an optimal solution in K14 while N55 must be 0, apart from some other constraints. The method I currently use consists of starting with entering 0 in K14, check whether N55 is 0, and if not, entering 0.5 in K14, check whether N55 is zero, and if not, entering 1.0 in K14, etc. etc. Excel will find the solution, but it takes a lot of time.

    The macro looks like this:

    Please Login or Register  to view this content.
    I was wondering whether there would be a faster method of obtaining my desired result. For example, if I know that the value must lie between 0 and 1000, could I use a macro the narrows down to the solution, instead of trial and error starting from 0?
    If so, could you help me with such code? Or perhaps, such code is already available?

    Please let me know if I do not comply with certain forum rules, as I do intent to.

    Thanks for your help!
    Pieter
    Last edited by pietermeulendijk; 12-20-2010 at 12:11 PM. Reason: Question solved

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Finding solution by narrowing down

    Hi Pieter,

    Before considering a VBA solution have you tried the standard Excel Solver functionality approach?

    Rgds
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Finding solution by narrowing down

    Have you tried using Solver? If you haven't already you may need to enble it through Tools...Add-Ins.

    Remember to use code tags when quoting code as per the forum rules.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  4. #4
    Registered User
    Join Date
    12-20-2010
    Location
    the Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Finding solution by narrowing down

    Yes, I have tried using Solver, but that gives an error: 'error in constraint or destination cells'.

    Perhaps, the calculations behind the applications

    Application.Run "Conversie"
    Application.Run "BBV_max"
    Application.Run "BBV_max_A12"

    are too complicated to have Solver find a solution.

    Would there be any other solutions?

    Regards,
    Pieter

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Finding solution by narrowing down

    Hi Pieter,

    Do any of the cell formulae in the constraint or destination cells evaluate with an error?

    Rgds

  6. #6
    Registered User
    Join Date
    12-20-2010
    Location
    the Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Finding solution by narrowing down

    No, they don't. The constraint formulae are optimizations on their own (they are macros using the goal seek function). That's why I think the Solver does not work.

    Wouldn't there be any other possibilities to work around the Solver?

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Finding solution by narrowing down

    What you have is a function, with input in K14 and output in K55 and you are looking for a zero of that function.

    If that function is it always increasing or always decreasing, you could use a binary search.
    This will also work if the function is continuous and the values at 0 and 1000 have different signs.

    Please Login or Register  to view this content.
    Last edited by mikerickson; 12-20-2010 at 11:12 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    12-20-2010
    Location
    the Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Finding solution by narrowing down

    Hi mikerickson,

    Thanks for your reply. I get the idea of the code. There is one thing that prevents it from working correctly in my case, however. In my first post, the code includes running application "Conversie". This application must be run intermediately and changes the result in N55. Here are its contents:

    Please Login or Register  to view this content.
    Where and how would you integrate this piece of code?

    Regards,
    Pieter

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Finding solution by narrowing down

    Then just put it in where needed

    Please Login or Register  to view this content.
    The linear search in the OP takes (up to) 2000 loops to find a 0 between 0 and 1000.
    A binary search will do it in 22 loops. Log(1000/.001)/Log(2)

    (the .001 is the closeness specified in the Loop Until condition.)

  10. #10
    Registered User
    Join Date
    12-20-2010
    Location
    the Netherlands
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Finding solution by narrowing down

    Thanks mikerickson,

    Works perfectly now, and much faster and more accurate than before!

    Case solved.

    Regards,
    Pieter

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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