+ Reply to Thread
Results 1 to 8 of 8

Solver For Loop

  1. #1
    Registered User
    Join Date
    01-16-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Question Solver For Loop

    Hey Awesome Excelerators!

    Hoping you can help a n00b out here with no VB knowledge at all. Already spun my wheels long enough!!

    Been looking at so many forums talking about using VB FOR Loop to run solver multiple times, easily, efficiently, and identify multiple target cells so I hope im on the right track.

    Doing this manually only for ONE target cell is a huge headache.. and I have over 200+ in a row within one worksheet.


    Target cell at $U$1, but I have targets cells from $U$1 all the way to $U$200.
    The variable cells currently consist of only $T$1, $O$1 also to $T$200, $O$200.
    I have a constraint where $U$XX must = $E$XX (Also to 200)

    Hope someone can show me the way?

    SolverOk SetCell:="$U$1", MaxMinVal:=1, ValueOf:=0, ByChange:="$T$1,$O$1", _
    Engine:=2, EngineDesc:="Simplex LP"
    SolverAdd CellRef:="$U$1", Relation:=2, FormulaText:="$E$1"
    SolverAdd CellRef:="$T$1", Relation:=4, FormulaText:="integer"
    SolverAdd CellRef:="$O$1", Relation:=4, FormulaText:="integer"
    SolverAdd CellRef:="$T$1", Relation:=1, FormulaText:="40"
    SolverOk SetCell:="$U$1", MaxMinVal:=1, ValueOf:=0, ByChange:="$T$1,$O$1", _
    Engine:=2, EngineDesc:="Simplex LP"
    SolverOk SetCell:="$U$1", MaxMinVal:=1, ValueOf:=0, ByChange:="$T$1,$O$1", _
    Engine:=2, EngineDesc:="Simplex LP"
    SolverSolve
    SolverReset

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: Solver For Loop

    Hello Hayashi151

    Firstly, I see it your first post so welcome to the forums

    There are a fdew rules to adhere to before the forum moderators get on your case

    Have a look at the rules about posting code in particular. I will add the tags this time before someone else is on your case, this time only

    It is always a great idea to attach an example of your problem, including expected results.

    For me, I work much better with a rel life spreadsheet (in .xlsm format with your macro progress to date) than with abstract code snippets

    bother, I cannot see the way to adjust your post, will put the code in this post instead

    Please Login or Register  to view this content.
    again, welcome to the forums, remember all who contribute have "real jobs" as well as helping us novices so be patient

    Jmac

  3. #3
    Registered User
    Join Date
    01-16-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Solver For Loop

    Hey jmac1947.

    Thank you so much for the FYI and taking the time to respond.

    Definitely appreciate the camaraderie. I went back to read the forum rules. Got it! Hopefully.

    Happy early birthday as well. I hope your Friday night will be pretty awesome.

    Attached is the workbook and I am hoping you and other see what I am trying to do.

    THANK YOU!

    Hope to visit Australia sometime.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    01-16-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Solver For Loop

    Thank you again for the warm welcome.

  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: Solver For Loop

    Try this macro and see if it works for you.

    Please Login or Register  to view this content.
    Alf

  6. #6
    Registered User
    Join Date
    01-16-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Solver For Loop

    Alf, you da man. Thank you so much for busting this out. Everything works well.
    I hope this didnt take too much time, but I see you are pro.

    Now I will review the code and try to understand it so that I may configure accordingly.

    In order to add another variable to solve the target cell, do I just copy/paste and change the variable like so?
    Not sure if I need the underscore "_" after the variable listing

    Please Login or Register  to view this content.
    Will have to guess what the "relation" numbers mean via the Solver Excel UI. Assuming its "<=, >=, etc"

    Also, totally excited to get help from Sweden. Another place I had always wanted to visit. Power of the internet.

    Thank you.

  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,758

    Re: Solver For Loop

    "_" after the variable listing
    The "_" is to show what really should be one line i.e.

    Please Login or Register  to view this content.
    is the way the line should be written but it's somtime easier to read macro by breaking up long lines and this goes for all macro writing (VBA Excel of cource)

    So for instance the line
    Please Login or Register  to view this content.
    could also be written as
    Please Login or Register  to view this content.
    I'm also adding a picture showing relationship.

    In order to add another variable to solve the target cell, do I just copy/paste and change the variable like so?
    Yes that looks right to me.
    Alf

    Ps Thanks for rep and comment!! No that's not against forum rules
    Attached Images Attached Images
    Last edited by Alf; 01-16-2014 at 05:11 PM. Reason: Forgot to answer an OP question

  8. #8
    Registered User
    Join Date
    01-16-2014
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Solver For Loop

    Solved. THANK YOU ALF!! Totally REPPED! +1

+ 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 loop
    By mat1973 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2013, 02:01 PM
  2. loop with solver
    By byueill in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-16-2012, 01:41 PM
  3. VBA Loop using solver.
    By ec41tp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2011, 05:10 AM
  4. Solver Loop
    By dabrows in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 02-18-2011, 01:42 PM
  5. Solver Loop
    By Boof in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-08-2011, 01:28 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