Automate Solver with VBA

  • I'm using this code to run Solver with VBA. When I run it, there is no solution (or Solver Results dialog box). I check the Solver under "Tools" and see that the constraints and MaxMinVal are set OK. But there are no entries for SetCell or ByChange. What have I done wrong?

  • Re: Automate Solver with VBA


    I've searched all over and I'm at my wits end with this. I ran the code and no solution was given. So I went to the Solver under Tools. Only the constraints as defined by the VBA code are defined. So I manually entered the cell references for SetCell and ByChange. Then I ran the Solver manually and BINGO... it worked fine. Does anyone have any ideas as to why my code is not giving a solution? I should add that something is happening because the screen flickers when I run the code. But I neither get the Solver Results dialog box or a solution in cells C11:C22.

  • Re: Automate Solver with VBA


    Thanks. I saw that thread in one of my searches. There's nothing there that suggests a resolution to my problem.

  • Re: Automate Solver with VBA


    Maybe my problem is not clear judging from the lack of responses. Attached is a screenshot of the Solver parameters after running the code. The constraints are fine. But I'm not able to get anything returned for "Set Target Cell" or "By Changing Cells". I found a site where someone else had this problem. Unfortunately, they (like me) had no replies posted back.

  • Re: Automate Solver with VBA


    I think your right about the problem being too difficult to solve without an example.
    Looks like you need to add the Address property to your statements.
    [vba]
    SolverOk SetCell:=Range("R23").address, MaxMinVal:=2, _
    ByChange:=Range("C11:C22").Offset(0, intOffset).address[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Automate Solver with VBA


    Thanks Andy. I had tried adding the Address before but it didn't help. Could I PM you the file to look at? It really perplexes me as to why it is not working.

  • Re: Automate Solver with VBA


    Tried the file you sent Andy and still the same problem You can get it to work and I can't which means it's not a coding problem. I think I'll get in touch with FrontLine Systems and see if they may know where the problem lies. :thanx:


    BTW - this was Andy's fix

    Code
    SolverOk SetCell:=Range("Total_Cost"), MaxMinVal:=2, _
             ByChange:=Range("Ship").Offset(0, intOffset).Address
  • Re: Automate Solver with VBA


    An update. It turns out that there is an issue with the SP-3. "Problem parameters are not set. For example, parameters such as Changing Cells or Target Cell are not set." http://support.microsoft.com/d…?scid=kb;en-us;821430#kb3 Although there is a patch to fix it, it means others may have the same problem when I try to share the app. Is there a way that I can set intOffset to be global so that I can manually put the cell references (named ranges) in the Solver and have it recognize the offset?

  • Re: Automate Solver with VBA


    As I don't have the problem I can not confirm whether this will work as a way around the problem although it does still work in for me.
    [vba] intoffset = currMonth - 1
    ThisWorkbook.Names("ShipOffset").Value = intoffset

    ' Set the target cell to a minimum value by changing cells C11:C22 or
    ' an offset of this range
    SolverOk SetCell:=Range("Total_Cost"), MaxMinVal:=2, _
    ByChange:=ThisWorkbook.Names("ShipOffsetRange").RefersToRange
    [/vba]
    Named ranges,
    ShipOffset: =3
    ShipOffsetRange: =OFFSET(Ship,0, ShipOffset)

    [h4]Cheers
    Andy
    [/h4]

  • Re: Automate Solver with VBA


    Thanks Andy. Could you clarify how to define the Named Ranges? (I tried the Insert - Name, but the code hangs at SolverOK with the message "Application-defined or object-defined error).

  • Re: Automate Solver with VBA


    Tried the file you sent and still no luck. I suspected as much since the problem lies with the SolverOK not being compatible with the version of Service Pack 3 that I'm using. I've been in touch with Microsoft Support and they provided me with a hotfix mentioned in the link I put in an earlier post. They warned that it hasn't been tested, to backup my system, etc etc. So what I've done to resolve my problem is to use an earlier version of solver.xla and everything works fine. Many :thanx: for your help. Without your feedback, I would have never realized that the problem was with a bug in the software and not a problem with my code.

  • Re: Automate Solver with VBA


    Having sorted out my original problem, I have a question regarding the use of solver. The code I'm using is below. This runs the solver one column at a time using an offset. What I have done is import another worksheet into the workbook. In this sheet, I want to run the solver using all columns at once (i.e. there is no offset or For... Next loop). The sheet has the identical same range names, but instead of being associated with 1 column (month), they are associated with 12 columns (year). Therefore, I would have 2 subs - RunSolver_Month and RunSolver_Year.


    I copied the code below and removed the offset. On each worksheet, I have a command button. For sheet1, I call RunSolver_Month and on sheet2 I call RunSolver_Year. The first sub runs fine but when I try to run RunSolver_Year, I get a message "Solver: An unexpected error occured or available memory was exhausted." I thought maybe it was because it didn't know which sheet the range names was referencing. Tried to put this in for example, but it didn't help.

    Code
    With Sheets("Main(2)")
        SolverOk SetCell:=.Range("Total_Cost"), MaxMinVal:=2, _
            ByChange:=.Range("Ship")
    end with


    I found that the program halts here:

    Code
    ' Solver Options...
        Call SolverOptions(MaxTime:=100, Iterations:=200, Precision:=0.0001, AssumeLinear _
            :=True, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _
            IntTolerance:=5, Scaling:=False, Convergence:=0.0001, AssumeNonNeg:=True)


    Any ideas as to what is wrong?



  • Re: Automate Solver with VBA


    I've narrowed the problem down. If I Rem the highlighted code below, it works fine. Unfortunately I can no longer Reset my Solver settings.

  • Re: Automate Solver with VBA


    I found a work-around. I set up the Options in the Solver dialogue. In the VBA code, removed the SolverReset and SolverOptions, then added a SolverDelete statement before each of the SolverAdd constraints. This last step ensures that I don't end up with replicated constraints each time I run Solver.

  • Solver VBA Isssues


    Just to let you all know that this thread has helped confirm my suspicions that the intermitant problems I have been having with solver when activated via vba are with solver and not with my code
    Thanks

  • Re: Automate Solver with VBA


    ...further.


    QuickDraw's link to MS's KB on this problem is the key. To reiterate, MS have a hotfix for Solver that works only(?) with Excel 2000 SP3, but you need to actually phone, fax or email them to get it. The whole process took 5 minutes from dialling to verifying it worked. Beautiful!
    KB Article Number(s): 821430, or search for "Excel Solver vba hotfix"


    My sordid history on this issue.
    Almost 10 years ago wrote a spreadsheet/vba application used in around 20 sites, in Excel 5... it worked fine, provided users could figure out how to install Solver.
    Upgraded to Excel 97. ...it worked fine, but sometimes on some machines Solver would quietly do nothing when called from vba. Could not resolve other that to use a different computer, which almost always worked.
    Upgraded to Excel 2000. I suspect Solver ALWAYS quietly did nothing when called from vba, but luckily it was not needed much, and as the writer I at least could figure out how to incorporate a manual Solver run in the use of the workbook.
    Eventually I thought I'd try to fix it a few days ago. I spent a hell of a lot of time debugging and tracking variables and monitoring and deleting hidden names, and learnt a quite a few things about ways to pass variables. I eventually decided the Solver addin was faulty. A few others on various forums have had the same problems, but it's only on OzGrid that I found the answer (which is not to say that it is not elsewhere, I just looked hardest here)


    I 'spose now I should see how it goes on 2002 and 2003.
    Duh!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!