+ Reply to Thread
Results 1 to 15 of 15

Solver VBA code across several sheets

  1. #1
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Solver VBA code across several sheets

    Hello everyone,

    I was wondering if there was a way to run solver across multiple sheets with just one button? For example, I have a file with around 100 sheets. This file has the same format (the data is placed in the same row/column in every sheet) for every sheet. Also I'm not really sure how to add constraints. Bellow, I will attach the excel file with the solver arguments.

    I tried to use the following code:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    Thanks in advance

  3. #3
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    I had to decrase the file size because of the attachment restricted size.

  4. #4
    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 VBA code across several sheets

    Maybe ...

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    I add some constraints. The code seems to be running but the results are wrong when compared to when I perform the solver procedure individually (for each sheet)


    Please Login or Register  to view this content.

  6. #6
    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 VBA code across several sheets

    Step through the code and look at the constraints in the Solver dialog before SolverSolve. Are they correct?

  7. #7
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    I think they are. I recorded the following macro:

    Please Login or Register  to view this content.

  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 VBA code across several sheets

    Not really what I asked.

    Put a Stop statement before SolverSolve. When the code breaks, open the Solver dialog and look carefully at the model. Is it correct?

  9. #9
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    I'm sorry, I donīt really understand. My knowledge in VBA is very basic.

  10. #10
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    I just recorded the macro so you could see the procedures I took

  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 VBA code across several sheets

    Right. You say you get different results when running via VBA versus the UI. So:

    1. Copy the original input values to re-use below, set up the model in the Solver dialog, and run Solver manually. Good result, correct?

    2. Restore the original input values and run the code from VBA. Bad result, correct?

    How could that be? Answer: Only if the model set in step 1 is different from that set via VBA. So,

    3. Restore the original input values, and step through the code until just before SolverSolve. Look at the model in the Solver dialog in the UI. Is it exactly the same as when you set it up manually?

  12. #12
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    I feel really dumb :P. I don't understand what you mean by original input values and stepping through the code until just before SolverSolve.

    After I run the code, the solver dialog is the same as before running the code (manually).

    After running the code:
    In the first sheet I get #NUM error in the target cell and the changing cells seem "normal". In the rest of the sheets I get a negative value in the target cell (which is not supposed to happen) and in the three changing cells I get 1's.

  13. #13
    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 VBA code across several sheets

    Post a workbook with a single worksheet that illustrates the problem, with ...

    o the Solver model manually configured and ready to go, so I can get the correct result just by pressing Solve

    o the code installed and ready to go that gives the errant result

  14. #14
    Registered User
    Join Date
    04-07-2016
    Location
    Portugal
    MS-Off Ver
    2007
    Posts
    20

    Re: Solver VBA code across several sheets

    Hey, I managed to run the code. I was missing one line of code. Bellow is the code I used. Thanks for your help shg!

    Please Login or Register  to view this content.

  15. #15
    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 VBA code across several sheets

    Well done, good job.

+ 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. Is there a way to use Solver via VBA code?
    By SandPounder1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2014, 11:53 AM
  2. Automating solver to run on multiple sheets
    By fonecorp in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-02-2013, 08:16 AM
  3. Automating solver to run on multiple sheets
    By fonecorp in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-26-2013, 05:40 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. [SOLVED] Solver, Promt box code
    By mike02 in forum Excel General
    Replies: 1
    Last Post: 07-24-2012, 02:36 AM
  6. Integrating Solver add-in, using VBA code
    By salilcheeran in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-21-2011, 01:23 PM
  7. [SOLVED] Help with Solver Code
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2005, 01:06 AM

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