+ Reply to Thread
Results 1 to 9 of 9

VBA: Solver - Sheet must be selected?

  1. #1
    Registered User
    Join Date
    01-10-2014
    Location
    Taipei
    MS-Off Ver
    Excel 2010
    Posts
    5

    Question VBA: Solver - Sheet must be selected?

    Hi

    I am fairly new to VBA and have just encountered a problem when dealing with the Solver Add-in.

    I'd like to run the below code without having to select the sheet "IRR".
    Unfortunately, it only works when "IRR" is selected (otherwise the solver results are posted in Cell B40 on the active sheet). How can I change the code so that it fits my needs?


    Please Login or Register  to view this content.

    Thanks a lot for your help!

  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: VBA: Solver - Sheet must be selected?

    The way solver is build it will only run from the active sheet so why not add a line to your macro after the "Dim" statment i.e.

    Please Login or Register  to view this content.
    Exctract from www.solver.com the makers of solver

    A limitation of the Solver is that all of the decision variables (adjustable or changing cells) in the By Changing Cells edit box must be cells on the active sheet. (This limitation makes the Solver considerably faster than if adjustable cells were allowed to be on any sheet.) You should re-design your Solver model so that all decision variables are on one sheet, and try again.
    Alf
    Last edited by Alf; 01-10-2014 at 09:38 AM. Reason: adding more info

  3. #3
    Registered User
    Join Date
    01-10-2014
    Location
    Taipei
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: VBA: Solver - Sheet must be selected?

    Quote Originally Posted by Alf View Post
    The way solver is build it will only run from the active sheet so why not add a line to your macro after the "Dim" statment i.e.

    Please Login or Register  to view this content.
    Exctract from www.solver.com the makers of solver



    Alf
    Hi Alf

    Thanks for your quick reply.

    That's actually what I already did...but then there is another problem.
    I want to automatically run the macro before the file (several selected sheets) is printed. Unfortunately, the macro then de-selects all sheets...
    Do you know how I'd have to adjust the code to re-select all sheets that have been selected before the solver part of the code?

    My current code looks like this:
    Please Login or Register  to view this content.
    Thanks and regards

  4. #4
    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: VBA: Solver - Sheet must be selected?

    Not really sure I understands your problem. How are the sheets selected and what stopps you for selecting them again after the Solver run?

    For example a macro like this will activate all sheets in a workbook one after another.

    Please Login or Register  to view this content.
    Alf

  5. #5
    Registered User
    Join Date
    01-10-2014
    Location
    Taipei
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: VBA: Solver - Sheet must be selected?

    Quote Originally Posted by Alf View Post
    Not really sure I understands your problem. How are the sheets selected and what stopps you for selecting them again after the Solver run?

    For example a macro like this will activate all sheets in a workbook one after another.

    Please Login or Register  to view this content.
    Alf
    Sorry, if I have not expressed myself clearly.

    The workbook contains several worksheets that are used to create a report. Depending on the requirements of the client/project different worksheets are used to create the report.
    The aim is that people can select (ctrl+left click) all the required sheets and then print the document or save it as PDF (thus, only the selected sheets are saved/printed). To make sure that all values are correct (and up to date) I want a macro to run before the file is printed or saved. The macro should, inter alia, include the code for the Solver Add-in. However, as I have to activate a specific sheet ("IRR") to run the solver, all the selected sheets (the sheets I want to print or save as PDF) are deactivated and will not be printed or saved as PDF.

    I'd need a macro that reactivates/reselects the sheets that were selected before the code to run the solver deactivated them.

    Any help is highly appreciated!

  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: VBA: Solver - Sheet must be selected?

    The simplest solution in my oppinion would be to first run Solver then let the people select the sheets to print.

    If that's not possible I think you may need a list box setup where selected sheets are added to an array then after the solver run the array is used to reselect the sheets.

    As arrays are really not my "cup of tea" I'm sorry but I can't help you any further.

    Alf

  7. #7
    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: VBA: Solver - Sheet must be selected?

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

  8. #8
    Registered User
    Join Date
    01-10-2014
    Location
    Taipei
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: VBA: Solver - Sheet must be selected?

    Thanks, shg!
    This works great!

    And also a big thank you to Alf for all the input.


    Quote Originally Posted by shg View Post
    Please Login or Register  to view this content.

  9. #9
    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: VBA: Solver - Sheet must be selected?

    You're welcome.

+ 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] Trying to copy selected items from sheet to listbox then selected back to sheet
    By BigWes1960 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 03-14-2013, 03:27 PM
  2. Replies: 2
    Last Post: 09-06-2005, 06:05 AM

Tags for this Thread

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