+ Reply to Thread
Results 1 to 7 of 7

Running solver for many cells

  1. #1
    Registered User
    Join Date
    11-12-2017
    Location
    india
    MS-Off Ver
    2007
    Posts
    3

    Exclamation Running solver for many cells

    Hi

    I am trying to solve around 1000 cells at the same time. I came to know that this is possible by using macro but I have been unsuccessful to implement it.
    I need the solution from AC3:AC1003, the constraints and formulas are already there. So I need to move to the next row after solving each one.
    Another problem is that a part of the variables will be shared by all. So if I get a optimised value for the AC3 cell , when i get the optimised value for AC4 , AC3 value gets changed. I figured this can be fixed if I add a macro after each solve to copy its text value to another cell.

    Can anyone help ? Thanks in advance
    Attached Files Attached Files
    Last edited by sumit86; 11-12-2017 at 11:34 AM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,844

    Re: Running solver for many cells

    This thread has an example of solver in a loop: https://www.excelforum.com/excel-pro...e-columns.html and also links to another thread with another example of Solver in a loop. Study those examples.

    This page has some general ideas and suggestions for calling Solver from VBA: https://peltiertech.com/Excel/SolverVBA.html

    I would start with those, make sure you can work those examples, then let us know what parts you are having difficulty with in adapting to your specific spreadsheet.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    11-12-2017
    Location
    india
    MS-Off Ver
    2007
    Posts
    3

    Re: Running solver for many cells

    Hey thanks for you help. I tried running the following code.

    Please Login or Register  to view this content.
    But I dont think it worked. I tried running the loop from AC3:AC50

  4. #4
    Registered User
    Join Date
    11-12-2017
    Location
    india
    MS-Off Ver
    2007
    Posts
    3

    Re: Running solver for many cells

    I rechecked some stuff and it worked ! Just another thing , as you can see from the code BD3:BD8 is common to all the the cells. So after the loop is completed all the cells take up the value chosen by the variables of the last one. Is it possible to copy the value of the cell to some other location and add it inside the loop ?
    Found a way never mind
    Last edited by sumit86; 11-12-2017 at 12:54 PM.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,844

    Re: Running solver for many cells

    You can use the .Value property of a cell (a range object: https://msdn.microsoft.com/en-us/VBA...e-object-excel ) to read and write a value from one cell to another. a Let statement like cells(i,30).value=cells(i,29).value would copy the value in the 29th column into the 30th column.

    Be careful with unqualified properties/methods like this though (even your current code is using an unqualified cells(...).address method/property). Unqualified references will refer to whatever the activesheet is. Only you will know for sure, but if there is any possibility that you don't want to run on the activesheet, it will be necessary to specify what sheet and/or book so that Excel runs the code on the correct block of cells.
    Last edited by MrShorty; 11-12-2017 at 12:57 PM.

  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: Running solver for many cells

    It is possible to simplify you macro a bit, i.e. when recording a macro Excel do often add some extra / duplicate lines. There is also possible to use Cell in stead of Cells.

    For Cells you have to use coordinates (rows and columns) and define this as address but one can also use letters and variables. As both works it is just a question of what is easiest to use and how to make macros as readable as possible.

    Please Login or Register  to view this content.
    This macro will write result from cell AC & i to cell AF & i as a value.

    Alf

  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: Running solver for many cells

    Oooooops!

    Just saw that the macro I modified post #3 did not correspond with the macro recorded in the uploaded file. A macro based on solver setting in the uploaded file should look like this:

    Please Login or Register  to view this content.
    Not sure I understand your solver settings. Range BD3:BD8 is set to binaries and the formula in range BD9 = SUM(BD3:BD8) and BD9 = 1

    I set up a small loop to see how the cells to change behaved i.e. range BD3:BD8 and got the following values

    Loop 1 BD4 = 1, all others 0 and sum BD9 = 1
    Loop 2 BD3 = 1, all others 0 and sum BD9 = 1
    Loop 3 BD4 = 1, all others 0 and sum BD9 = 1
    Loop 4 BD4 = 1, all others 0 and sum BD9 = 1
    Loop 5 BD4 = 1, all others 0 and sum BD9 = 1
    Loop 6 BD4 = 1, all others 0 and sum BD9 = 1
    Loop 7 BD4 = 1, all others 0 and sum BD9 = 1

    So I do wonder do you really need solver?

    Alf

+ 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. Replies: 0
    Last Post: 09-22-2015, 06:09 PM
  2. [SOLVED] Running solver several times using a for-loop
    By sherm.j7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-02-2015, 01:06 AM
  3. Problem when running Solver
    By alexfex in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2014, 07:03 AM
  4. running Solver in vba with SheetChange
    By BHudPE in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-23-2010, 11:48 PM
  5. Solver error when running an optimizer
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-27-2009, 10:17 AM
  6. running solver from VBA
    By Clavier in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-22-2009, 10:38 PM
  7. Running Solver from Function
    By krepa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2006, 02:07 PM

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