+ Reply to Thread
Results 1 to 5 of 5

One Macro with multiple solver each with different constraints

  1. #1
    Registered User
    Join Date
    11-01-2013
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    3

    One Macro with multiple solver each with different constraints

    Hello,

    I have a problem:

    I have a first optimization problem with constraint set 1 where I use solver. I've saved these constraint in some cells. Now, based on this solution, I compute a second optimization problem but with an other 'objective function', 'cells to change' and 'constraint set' (set 2, which I also saved in the file).
    Now all of this I want to record in a macro:
    - load constraits set 1
    - fill objective function 1
    - fill in variables to change 1
    - solve
    - load constraints set 2
    - fill in objective function 2
    - fill in variables to change
    - solve

    When I'm recording this, It is working but when I want to use the macro, it fails. I think it won't recognize the 'load of constraint' but I don't knw how to solve it. Can you please? I would like it if I can solve the problem in the same sheet.

    This is my code:
    ----------------------------------------------
    Range("J2:O22").Select
    Selection.ClearContents

    SolverOk SetCell:="$Y$23", MaxMinVal:=1, ValueOf:=0, ByChange:="$J$2:$O$22", _
    Engine:=2, EngineDesc:="Simplex LP"
    SolverSolve

    Range("AH2:AL22").Select
    Selection.ClearContents

    SolverLoad LoadArea:="$J$2:$O$22"
    SolverOk SetCell:="$AF$23", MaxMinVal:=1, ValueOf:=0, ByChange:="$AH$2:$AL$22" _
    , Engine:=2, EngineDesc:="Simplex LP"
    SolverSolve
    -------------------------------------
    Last edited by Alyotter; 11-01-2013 at 02:13 PM.

  2. #2
    Registered User
    Join Date
    11-01-2013
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: One Macro with multiple solver each with different constraints

    Oke, I'm getting forward... But after the first 'solve' it doesn't start the second solve and it says: "setting up the problem"

    This is my new code:
    ---------------------------------------------
    Range("J2:O22").Select
    Selection.ClearContents

    SolverOk SetCell:="$Y$23", MaxMinVal:=1, ValueOf:=0, ByChange:="$J$2:$O$22", _
    Engine:=2, EngineDesc:="Simplex LP"
    SolverLoad LoadArea:=Range("B33:S33"), Merge:=False
    SolverSolve UserFinish:=True

    SolverReset

    Range("AH2:AL22").Select
    Selection.ClearContents

    SolverOk SetCell:="$AF$23", MaxMinVal:=1, ValueOf:=0, ByChange:="$AH$2:$AL$22" _
    , Engine:=2, EngineDesc:="Simplex LP"
    SolverLoad LoadArea:=Range("B34:H34"), Merge:=False
    SolverSolve UserFinish:=True

  3. #3
    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,758

    Re: One Macro with multiple solver each with different constraints

    Looking at your code the only thing I'm unsure of is the line

    Please Login or Register  to view this content.
    I do wonder if there could be some parameters missing or the command "SolverReset" clears that range? Have you tried blanking out that line and see if the macro runs the second time?

    Have a look at this link about "LoadArea" command

    http://www.solver.com/content/basic-...rload-function

    Alf

    Ps In the future don't forget to wrap your code.

    You also don't need to use "Select" and "Selection" insted use the commands "directly" i.e. :

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-01-2013
    Location
    Kortrijk, Belgium
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: One Macro with multiple solver each with different constraints

    First of all: thanks for the respond!!
    1) But what is 'to wrap'? What is its function and how can I do it? (I'm new here, sorry...)
    2) I have deleted the 'SolverReset' and it didn't help. But I looked at some examples on the internet and placed 'SolverLoad' before 'SolverOk' and then it worked. I don't know why this works and in the other order not...

    Alyotter

  5. #5
    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,758

    Re: One Macro with multiple solver each with different constraints

    But what is 'to wrap'?
    If you hover with your mouse pointer over the # icon you see a text "Wrap [Code] tags around selected text" so you click on that icon and you get these:

    [CODE][CODE] and you place your macro code between those two blocks. For quoting text you click on the first icon to the left of the # icon.

    So placing "SolverLoad" before "SolverOk" fixed your problem. Good to know.

    I don't know why this works and in the other order not...
    Well solver works in mysterious ways it's miracles to perform!

    Finally even if you solved your problem yourself could you still mark this thread "Solved"

    Alf

    Ps
    To mark your thread solved do the following:
    New Method
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved

+ 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. Using solver with constraints
    By Hadoushi in forum Excel General
    Replies: 4
    Last Post: 03-15-2013, 08:17 PM
  2. Run Solver Multiple Times (while changing constraints and goal for each iteration)
    By pmw1218 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-03-2013, 04:17 PM
  3. How to use Solver constraints : dif
    By alee001 in forum Excel General
    Replies: 0
    Last Post: 06-06-2012, 02:36 PM
  4. Set Solver constraints to multiple data values
    By DutchDude in forum Excel General
    Replies: 3
    Last Post: 01-17-2012, 03:11 AM
  5. Solver constraints shall be OR // Solver target cell
    By Cunner in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2009, 04:28 AM

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