+ Reply to Thread
Results 1 to 12 of 12

SOLVER "error in model" error from VBA

  1. #1
    Registered User
    Join Date
    05-09-2013
    Location
    Michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    SOLVER "error in model" error from VBA

    Hello Excel VBA experts,

    I have a multi-variable problem with multiple constraints that is solved successfully and quickly with SOLVER directly in Excel. However, I have the following issues when trying to do the same within a loop in VBA.

    Firstly, recording a macro while executing SOLVER was little help for determining the proper syntax. I pieced code together from examaples found on-line.

    I do have a reference set to SOLVER within VBA by Tools<References<check the "solver" box. However, I get compile errors using lines like:

    Please Login or Register  to view this content.
    If I preceed the solver commands with Application.Run, put the commands within quotes, and omit the text preceeding the arguments (SetCell:=, for example), I avoid the compile errors. Here is my code:

    Please Login or Register  to view this content.
    Each line executes, but an "error in model" is generated after the first SolverSolve command.

    Why do I need Application.Run?
    What is wrong with the syntax in my example? The column references in the SolverAdd commands are correct.


    I'm using Excel 2010 with Windows XP if that matters.

    Thanks for suggestions.

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

    Re: SOLVER "error in model" error from VBA

    Is it possible to upload a sample file?

    Alf

  3. #3
    Registered User
    Join Date
    05-09-2013
    Location
    Michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: SOLVER "error in model" error from VBA

    Alf,

    I didn't attach the file because it is very large with many pages of code. I'll try to extract just the troubled parts and post later today.

    thanks
    T

  4. #4
    Registered User
    Join Date
    05-09-2013
    Location
    Michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: SOLVER "error in model" error from VBA

    Here is a reduced spreadsheet that shows the same behavior. A screen shot of how the problem is posed directly in Excel is in the sheet. Hit the 'solve it' button to execute the macro. A solution is found when run from Excel. An 'error in model' is returned when attempting to solve from VBA. The loop in VBA does start on row 21.

    thanks,
    T
    Attached Files Attached Files

  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 "error in model" error from VBA

    Very strange!

    A macro like this should be sufficient to run Solver after adding "Solver" in "Tools" -> "References"

    Please Login or Register  to view this content.
    But when I try to run it I get the error message "Compiler Error: Can't find project or library" even if I've reset my solver reference! And running Solver from Excel with your proposed values works just fine!

    I've even blocked the "Private Sub" from Peltier Technical Services just in case this set a reference to Solver.xlam in another folder than where I have this file but no go.

    Alf

  6. #6
    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 "error in model" error from VBA

    I now got the macro to run, my previous "Sub Macro3()" was a bit wrong. This is the way it should look:

    Please Login or Register  to view this content.
    I started by saving your uploaded file as an ".xlsx" file in order to get rid of all macros and references to Solver. I then added my macro "Sub Macro3", set a reference to Solver and tested if if worked as it did.

    The next step should be to add your origanal macros one at a time and see if "Sub macro3" still works. I do suspect the "Private Sub" is setting a reference to Solver in a folder that was used for an older Excel version.

    Update:

    Have tested after importing modules "Module_4_predict_damping" and "Module_public_variables" and still I have no problem running "Sub Macro3".

    I leave the rest of this testing to you.

    Alf
    Last edited by Alf; 03-12-2014 at 02:42 PM. Reason: additional information

  7. #7
    Registered User
    Join Date
    05-09-2013
    Location
    Michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: SOLVER "error in model" error from VBA

    What recommendations are there about which extensions to use with and without macros and with which versions of Excel? I have long been using only .xls extensions with and without macros in 2003 AND 2010 versions of Excel with no issues. I converted the attachment to an .xlsm to be consistent with recent versions not because I use the .xlsm extension regularly.

    There is some peculiar behavior here. I saved the .xlsm file as an .xlsx in order to follow your steps exactly and was prompted that "VB projects can not be saved in macro free workbooks". If you go ahead and save, the macros and reference to Solver stay -unexpected. If you close and re-open the .xlsx, the macros and reference to Solver are gone. You then have to rebuild the macro and re-set the Solver reference. Then, macro3() works.

    When you save the .xlsm as an xls, you don't get prompted that "VB projects can not be saved in macro free workbooks". The macro and reference to Solver stays after the save-as, but macro(3) does not work. If you close and re-open, the macros and reference are still there, and macro3() still doesn't work. Only if you remove the macro and reference and re-create them will macro3() work.

    So if I want to add Solver commands to macros and make the workbooks usable to those who are still using Excel 2003, I have to remove all modules, delete the reference to Solver, then re-import all modules, re-reference Solver, then save as an .xls. Does this seem right?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: SOLVER "error in model" error from VBA

    Entia non sunt multiplicanda sine necessitate

  9. #9
    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 "error in model" error from VBA

    I suspect you problem are set by the “Private Sub Workbook_Open()” where a reference is set to solver.

    Perhaps you could test with a “clean” workbook adding all you modules and then “manually” set the solver vba references for the different environment you wish to test it in excel 2003, excel 2007 and excel 2010.

    Since solver needs a reference set to the "Solver.xlam" file to work in a macro you have to take in account that in excel 2010 this file is normally found in

    "C:\Program Files\Microsoft Office\Office14\Library\Solver\Solver.xlam".

    If on the other hand you are running Windows 7 64 bit as you OS and your Office version is 32 bit then the file is found at:

    "C:\Program Files\Microsoft Office (x86)\Office14\Library\olver\Solver.xlam".

    For Excel 2007 the Solver.xlam file if normally found in this folder:

    "C:\Program Files (x86)\Microsoft Office\Office12\Library\SOLVER\SOLVER.XLAM"

    And finally for excel 2003 the Solver.xla file is found in the folder:

    "C:\Program Files (x86)\Microsoft Office\OFFICE11\Library\SOLVER\SOLVER.XLA"

    As you model is build as a "GRG-Nonlinear" it should work in all 3 environment (excel 2003, excel 2007 and excel 2010) with the right solver reference and if you let the user set the reference then there should be no problem in my opinion.

    Alf

  10. #10
    Registered User
    Join Date
    05-09-2013
    Location
    Michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: SOLVER "error in model" error from VBA

    shg,

    I tried multiple times getting compile errors with the VB Solver commands each time. In the end, I had to:

    - Use CodeCleaner to export all code
    - manually delete each module, form, and code on individual worksheets
    - Save
    - Close and re-open file
    - Create a dummy module (just so there is some VB for CodeCleaner to work)
    - Use CodeCleaner to import all code files
    - re-set the reference to Solver

    and now macros with the Solver commands work in my .xls workbook.

    thanks
    T

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: SOLVER "error in model" error from VBA

    I think CodeCleaner does all of those thing all by itself -- removes all of the code, saves the workbook, and reimports the code -- so I'm not sure what you're doing that it doesn't already do. But I'm glad you got it sorted.

  12. #12
    Registered User
    Join Date
    05-09-2013
    Location
    Michigan, USA
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: SOLVER "error in model" error from VBA

    It looks like it does do the remove, save, and re-import, but it does not close after saving and re-open. This I had to do manually to avoid compile errors. The reference to solver does not seem to get recognized unless the file is saved and closed with the reference turned off, then re-opened and re-set. Weird

    thanks for the help. I'll mark this as solved

    T

+ 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. [SOLVED] If Not ActiveSheet.Range("A1").Value Like "apple" Then MsgBox "Error"
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-18-2014, 02:16 PM
  2. Solver add-in not working - error in model
    By Coaster in forum Excel General
    Replies: 5
    Last Post: 04-27-2013, 11:49 PM
  3. [SOLVED] VBA Excel 2007 : Autofit Merge Cells Error "Run-time error '13': Type Mismatch"
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-18-2012, 04:10 PM
  4. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  5. [SOLVED] What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 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