+ Reply to Thread
Results 1 to 13 of 13

How to loop the solver in the VBA?

  1. #1
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Cool How to loop the solver in the VBA?

    Hello guys i desperately need some help!I am working on a very simple formula on solver but i would like to repeat for the next rows and i simply fail every time i try it out!Pls helpppppppppppp!!!!!!!!!!!

    Please Login or Register  to view this content.
    This is the main body.I just want it to solve for cells C16,C17,C18 etc with the respective references....again it will be D16:M16 and N16 etc,,,
    Last edited by nchinas; 02-28-2014 at 08:09 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: VBA and solver

    Hi and welcome to the forum

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    I will add the tags for you - this time
    Last edited by FDibbins; 02-28-2014 at 07:36 PM. Reason: edited at the request of a senior member
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  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: How to loop the solver in the VBA?

    Perhaps somthing like this would work for you?

    Please Login or Register  to view this content.
    This loop starts at row 15 and loops 4 times (till i is 18). If you wish to exstend the range change the stop value of i.

    Alf

  4. #4
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: How to loop the solver in the VBA?

    You are amazing...a million thanks won't be enough!

  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: How to loop the solver in the VBA?

    Glad to be of help and thanks for feed back.

    Alf

  6. #6
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: How to loop the solver in the VBA?

    Alf i introduced what you proposed and it works fine but up to a point.The problem is that i cannot actually spot where the problem lies.If i post the excel file do you think that you can help me?

  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: How to loop the solver in the VBA?

    If i post the excel file do you think that you can help me?
    Sure no problem just also tell me what kind of problem you have and I'll have a go at it.

    Alf

  8. #8
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: How to loop the solver in the VBA?

    Optimal.xlsmOk i will provide a sort description.The excel file is very simple.It has the returns of 8 stocks the var-covar matrix and the weights.I run the macro as you showed me but solver produces the same values after a point.Now the problem is that as i examine it i come up to the conclusion that it might be a problem of economic nature rather than a calculation problem!Anyway i am looking forward for your reply along with any improvements about VBA!

  9. #9
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: How to loop the solver in the VBA?

    http://www.youtube.com/watch?v=fa3TG4ZpJY8 i actually tried to reproduce this video!!

  10. #10
    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: How to loop the solver in the VBA?

    I think as you do that this is a maths problem not a solver problem. See my comments on sheet2.

    I've also made some slight changes to the macro does not improve anything just makes the macro a bit easier to write. Also made a correction to the line

    Please Login or Register  to view this content.
    and changed it to

    Please Login or Register  to view this content.
    You did not have any problem because you that line was written twice in the original macro (my bad really) but you only need to write it once and then you add it just after the "SolverReset" line.

    Checked the youtube link but could not hear any comments so I'm a bit baffled how he set it up.

    Alf
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: How to loop the solver in the VBA?

    Alf you did help me a lot!!!!As i told you before i am more than convinced that this is a financial problem and not a calculation one!I did not understand the part with the solver options though....

  12. #12
    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: How to loop the solver in the VBA?

    convinced that this is a financial problem and not a calculation one
    Yes in the sense that starting from row 35 solver can't meet the constraint A35 = B35 and the rest of the solver result are probably infeasible i.e. rows 36 to 42. You could try to run rows 35 to 42 "manually" and see what solver thinks about them. Problem with running solver in a macro with "UserFinish" and "SolverReset" is that you will not see if solver can't find a solution. You must analyze the result.

    Offhand I would guess that the function in B has a max value around 0,62 have you tried to plot the B values in a graph?

    did not understand the part with the solver options though....
    Before running solver with a macro you can "open" solver and set the options you wish like "Max time", "Number of iterations" and "Assume non Negtive" and as these settings are not changed by the "SolverReset" command you don't have to add them in the macro.

    Alf
    Last edited by Alf; 03-01-2014 at 04:58 PM.

  13. #13
    Registered User
    Join Date
    02-28-2014
    Location
    Greece,Athens
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: How to loop the solver in the VBA?

    Yes Alf i did a little research...Unless i remove the short selling restrictions it is impossible to get a return higher than that of the individual stock.Again many many thanks for your help!!!!

+ 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