+ Reply to Thread
Results 1 to 8 of 8

VBA Solver for Multiple Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2020
    Location
    Newark, USA
    MS-Off Ver
    2016
    Posts
    3

    Exclamation VBA Solver for Multiple Rows

    Hello,

    I am trying to solve a cost optimization problem related to electricity generation and load. I am having trouble in editing the solver code for multiple rows (from 5 to 8764). I have tried this but this does not work and is showing error:

    Sub Macro1()
    Dim i As Integer
    ActiveWorkbook.ActiveSheet.Activate
    For i = 5 To 8764
    
        SolverOk SetCell:="$L$" &i, MaxMinVal:=2, ValueOf:=0, ByChange:="$G$" & i &"$J$"&i, _
            Engine:=1, EngineDesc:="GRG Nonlinear"
        
        SolverAdd CellRef:="$G$" & i, Relation:=1, FormulaText:="$V$25"
        
        SolverAdd CellRef:="$G$" & i, Relation:=3, FormulaText:="$U$25"
      
        SolverAdd CellRef:="$H$" & i, Relation:=1, FormulaText:="$V$26"
        
        SolverAdd CellRef:="$H$" & i, Relation:=3, FormulaText:="$U$26"
       
        SolverAdd CellRef:="$I$" & i, Relation:=1, FormulaText:="$V$27"
        
        SolverAdd CellRef:="$I$" & i, Relation:=3, FormulaText:="$U$27"
        
        SolverAdd CellRef:="$J$" & i, Relation:=1, FormulaText:="$V$28"
       
        SolverAdd CellRef:="$J$" & i, Relation:=3, FormulaText:="$U$28"
    
        SolverAdd CellRef:="$K$" & i, Relation:=2, FormulaText:="$D$" & i
      
        SolverSolve = True
    
    End Sub
    Last edited by Pepe Le Mokko; 01-15-2020 at 03:20 AM. Reason: Add code tags and remove urgency

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

    Re: Need urgent help with VBA Solver for Multiple Rows

    Welcome to the forum.

    You should wrap your code as some moderators gets really upset if you don't do.

    Tray to change

    ByChange:="$G$" & i &"$J$"&i,
    to this and see if it helps
    ByChange:=”$G$” & i & ":$J$" & i,
    Alf

  3. #3
    Registered User
    Join Date
    01-15-2020
    Location
    Newark, USA
    MS-Off Ver
    2016
    Posts
    3

    Re: Need urgent help with VBA Solver for Multiple Rows

    Thank you Alf. The variable cells in my worksheet are from G to J. I tried to solve it using your method. The code runs and shows a solution but comes back to the VBA window and shows a Run-time Error '424': Object Required. When I hit debug it highlighted the SolverSolve = True.

  4. #4
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,621

    Re: Need urgent help with VBA Solver for Multiple Rows

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    I did it for you this time. Please read forum rules. Thanks

  5. #5
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,621

    Re: VBA Solver for Multiple Rows

    Administrative Note:

    If you have an urgent request you could contact our Commercial Services
    Please remember that those who help here do so voluntarily and of their own goodwill. They live in many different time zones and many will be out at work right now if they are not asleep on the other side of the globe. This is not a paid 'service': members will help when they are ready and able to do so, and not 'on demand'. Please do not try to put pressure of time on anyone here: if you get the help you need today, that's fine, but you really should not count on it.
    Thanks for your understanding and patience.

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

    Re: VBA Solver for Multiple Rows

    highlighted the SolverSolve = True
    So you either change that to:
    SolverSolve True
    or you could use
    SolverSolve UserFinish:= True
    The variable cells in my worksheet are from G to J.
    so if the range you change is cells G, H, I and J with different values of i then the code line I specified in post #2 is the one you need.

    As excel is notorious for adding extra code when using the macro recorder (still it's a good way to learn) your code could be trimmed a bit more i.e.

    Engine:=1, EngineDesc:="GRG Nonlinear"
    can be changed to

    Engine:=1
    or

    EngineDesc:="GRG Nonlinear"
    Alf

  7. #7
    Registered User
    Join Date
    01-15-2020
    Location
    Newark, USA
    MS-Off Ver
    2016
    Posts
    3

    Re: VBA Solver for Multiple Rows

    Thanks for the prompt reply Alf. I tried using your method and it worked but the optimization shows only zero value in the variable cells. Do you think there is any other method to select ranges other than "& i" ?

  8. #8
    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,785

    Re: VBA Solver for Multiple Rows

    optimization shows only zero value in the variable cells
    What value do you get if you run solver manually using say i equal to 2 in all cells?

    Another thing you should add to your macro since you are running solver in a loop is the command "SolverReset" this you add before the line "SolverOk SetCell:="

    If this does not solve your problem I would recommend you upload your file (just 10 - 15 rows with values will do) so it can be tested. Don't forget this is an open forum so check that the uploaded file don't contains any sensitive information.

    I'm a bit puzzled as you have a "For" loop for the row values (i) but I can't see a "Next" statement after the "SolverSolve" command to make the macro loop back to the next i value?

    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. [SOLVED] Solver in a Loop for multiple rows by changing multiple columns
    By glauberpontes in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-30-2018, 01:45 PM
  2. Running Solver on Multiple Rows
    By Boilermaker24 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-16-2018, 03:29 PM
  3. Need URGENT help please - SOLVER
    By targus92 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-21-2016, 06:06 AM
  4. Urgent Help Needed! - Deleting rows by multiple conditions
    By shivboy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-01-2014, 04:54 AM
  5. [SOLVED] Using "SOLVER" to match two columns, but how to copy it for multiple rows ??? (URGENT)
    By hshahad in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-16-2013, 11:31 AM
  6. Need urgent help with solver. It does not change value
    By mikelowrey36 in forum Excel General
    Replies: 1
    Last Post: 04-09-2013, 08:09 AM
  7. Replies: 1
    Last Post: 03-17-2013, 06:52 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