+ Reply to Thread
Results 1 to 15 of 15

Solver+VBA

  1. #1
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Solver+VBA

    Hi everyone,

    I need some help in solver function. I need to create a dynamic solver, that recalc every time the target of the function change.

    Imagine this scenario:

    The cell C2 has a function like "=IF(W4="";V4-V5;W4-V5)" and i want to use the solver for different vallues of cell C2 like Function=C2. Probably i will need a macro or something like that.

    thks in advance
    best regards

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Re: Solver+VBA

    The cell C2 has a function like "=IF(W4="";V4-V5;W4-V5)"
    Solver is "normally" not happy with discontinuous functions i.e. "IF" and "CHOOSE" to name a few. The nonlinear GRG Solver used in Excel can sometimes deal with discontinuities which are "incidental" to the problem, but as a general statement, the nonlinear Solver cannot be expected to find optimal solutions to such problems. However, the Evolutionary Solver included in Frontline's enhanced Solver products can make progress on these problems.

    I think the best solution would be a macro that checks the status of W4 probably a "Private Sub Worksheet_Change(ByVal Target As Range)" and if W4 = "" run solver1 macro else run solver2 macro.

    Alf

  3. #3
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA

    I´m not sure you understood my question.

    What i want is to make a routine that changes the content of this field (image) and recalc the function.

    Capturar.JPG

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,460

    Re: Solver+VBA

    The way I usually set this kind of problem up in Solver is to have a "helper" cell for the Set Target Cell. C3 can contain the target value, and C4 can be C2-C3. Then your Solver model becomes "Set Target Cell" C4 "to a value of" 0 by changing whatever C2 is dependent on. With this kind of model, your target value is always 0, so there will be no need to change the Value of field.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA

    Capturar.JPG

    i did no made myself clear. Hope this image help you understand.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,460

    Re: Solver+VBA

    I guess we're still not understanding exactly what the problem is.

    You have given two equations for C4 -- In post one, you have indicated that it is a discontinuous IF() type function, and Alf explained how Solver tends not to like those kind of functions. In post five, C4 looks more like a simple algebraic equation. So maybe the first step in understanding your question is exactly what is in C4?

    The second part of understanding your question is trying to understand exactly what you are using Solver to do. Solver is an excellent tool for finding roots of equations. If the expression in post 5 is correct, I would be tempted to suggest that the easiest way to solve that kind of equation is to solve it algebraically and program that solution into the spreadsheet -- Solver would not even be needed or used.

    A sample spreadsheet would probably make this easier to understand than pictures.

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,791

    Re: Solver+VBA

    Right click on the sheet where you have the C4 calculation and click "View Code". In the new windows that opens paste this code to it.

    Please Login or Register  to view this content.
    As soon as C4 changes i.e. any change made to either A1, C2 or B5 will run a macro called "test". So change this name to whatever you wish to call your solver macro.

    This of course assumes that C4 =(C2*A1^2+B5)/B5

    It's also possible to substitute the line "Run test" with your solver macro. This is just a matter of personal preferences.

    Alf
    Last edited by Alf; 05-07-2014 at 12:14 PM. Reason: Wrong code given

  8. #8
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA


  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Solver+VBA

    Like the others, a file would help us help you better.

    See if this gives you an idea or two.

    Please Login or Register  to view this content.
    When using the worksheet change event, I have done it like this:
    Please Login or Register  to view this content.
    This can also be handy.
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA

    Sorry for all the confusion arround my question, please report to de XLS.

    https://www.dropbox.com/s/7emvsp85os...res%20EC2.xlsm

  11. #11
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA

    Sorry for all the confusion arround my question, please report to de XLS.

    https://www.dropbox.com/s/7emvsp85os...res%20EC2.xlsm

  12. #12
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Solver+VBA

    Right click your sheet VED's tab, View Code, and paste. Change X12 to 0 and 10 to see the two solutions for the X variable in X14's formula. Be sure to add the Solver reference as I detailed earlier in comments of similar code below.

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA

    Thanks for you reply.

    I didn't really understand what you meant about: "Be sure to add the Solver reference as I detailed earlier in comments of similar code below."
    I´m not familiarized with VBA language.Could you please paste the exact code.

  14. #14
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Solver+VBA

    Adding this reference can take an extra step as I detailed before and here again. You have never added a reference? If not, see: http://excel-macro.tutorialhorizon.c...es-vbaproject/

    ' Add the Solver Reference:
    ' In the VBE, Tools > References, paste this in the File name box:
    ' C:\Program Files\Microsoft Office\OFFICExx\Library\SOLVER
    ' after replacing xx with your version of Excel (e.g., 11 for Excel 2003, 12 for Excel 2007, 14 fir Excel 2010).
    ' Change the file type dropdown to *.*, and pick SOLVER.XLA or SOLVER.XLAM
    ' You can't set a reference when code execution is suspended, so press the reset button (or do Tools > Reset), and then try.

  15. #15
    Registered User
    Join Date
    05-06-2014
    Location
    Portugal
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Solver+VBA

    Step by step...
    -I added the solver reference .XLAM
    -Pasted the code for the VBA console:
    Capturar.JPG
    And now??

+ 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. macro not keeping solver solutions when solver is successful
    By jimmypants in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2014, 01:45 AM
  2. Replies: 6
    Last Post: 05-18-2013, 05:49 AM
  3. Can't start Solver. Error message says Solver.xlam already open.
    By DaveHills in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 1
    Last Post: 10-21-2012, 11:02 AM
  4. solver macro + simulation code + not updating solver values
    By sabinemaria in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2012, 11:37 AM
  5. Interesting Solver problem (Solver encounters an error)
    By MrShorty in forum Excel General
    Replies: 3
    Last Post: 12-22-2005, 06:55 PM

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