+ Reply to Thread
Results 1 to 6 of 6

Completely new to VBA, need help on setting up a solver loop

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2017
    Location
    England
    MS-Off Ver
    2016
    Posts
    3

    Completely new to VBA, need help on setting up a solver loop

    I need help on how to set up a loop that solves a column of objective cells vs a column of variable cells.

    It doesn't matter that type of solver it is, "GRG Nonlinear" seems to work fine.

    My objective cells are E7:E207
    Where the solution is minimized or =0 because I want a real root.

    My variable cells are D7:D207

    The variables that it is solving are a cubic equation where it outputs the real root (no imaginary parts). I've looked around the forum but I can't really seem to make much sense. Any help would be much appreciated!

    I've made a basic macro so far
    Sub Solver()
    '
    ' Solver Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+S
    '
        Range("E8").Select
        SolverOk SetCell:="$E$7", MaxMinVal:=3, ValueOf:=0, ByChange:="$D$7", Engine:=1 _
            , EngineDesc:="GRG Nonlinear"
        SolverOk SetCell:="$E$7", MaxMinVal:=3, ValueOf:=0, ByChange:="$D$7", Engine:=1 _
            , EngineDesc:="GRG Nonlinear"
        SolverSolve
    End Sub
    tsumannai
    Last edited by tsumannai; 01-23-2017 at 12:13 PM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,462

    Re: Completely new to VBA, need help on setting up a solver loop

    Does it have to be Solver? I built a tutorial spreadsheet showing how to find roots of polynomials directly in the spreadsheet using circular references with iteration turned on: http://www.excelforum.com/showthread.php?t=1111494 The NR algorithm for polynomials is quite simple. This may be a good approach if you and others who will use the spreadsheet are not bothered by having iteration enabled.

    Here's another recent thread looping through a Solver algorithm. http://www.excelforum.com/showthread.php?t=1166949 The OP recorded a macro calling Solver, which Alf then adapted to a loop in post #3. I modified the code in post #17 to loop across columns instead of down rows, and Alf adapted it in post #20 to run on visible rows. What parts of these Solver loops do you have trouble understanding or implementing?
    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
    01-23-2017
    Location
    England
    MS-Off Ver
    2016
    Posts
    3

    Re: Completely new to VBA, need help on setting up a solver loop

    I played around with the script in the second link you sent me i think I managed to do it. Could you just skim over it to check if it's fine?

    From column D and E and rows 7-107.

    Sub Solver()
    '
    ' Solver Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+S
    '
    Dim i As Long
    Set ws = ThisWorkbook.Sheets(1)
    For i = 7 To 107
    
    SolverReset
    
    SolverOk SetCell:=ws.Cells(i, 5), MaxMinVal:=3, ValueOf:=0, ByChange:=ws.Cells(i, 4)
    
    SolverSolve Userfinish:=True
     'enters debug mode on each loop so I can see what is happening as the code runs. This will be removed in the final version when all debugging is done
    Next
    
    End Sub

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,462

    Re: Completely new to VBA, need help on setting up a solver loop

    After a quick skim over, it looks good. I notice that you removed the Stop statement I had in there. Does that mean that you stepped through it in the debugger enough times to be confident that it runs correctly?

  5. #5
    Registered User
    Join Date
    01-23-2017
    Location
    England
    MS-Off Ver
    2016
    Posts
    3

    Re: Completely new to VBA, need help on setting up a solver loop

    It ran fine without the stop statement and the results that I plotted seemed to match what It was meant to be. (physically in the real world.)

    Thankyou very much for helping me out, I've definitely learnt a few things today!

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,462

    Re: Completely new to VBA, need help on setting up a solver loop

    Glad it is working.

    As someone who is "completely new to VBA", I would encourage you to put a Stop statement back into the procedure and spend some time becoming familiar with VBA's debugging tools, if you did not spend much time in the debugger already. Half of programming is writing the code, and the other half of programming is debugging the code -- including becoming familiar with available debugging tools.

+ 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. Setting the solver properly
    By altaris in forum Excel General
    Replies: 3
    Last Post: 06-28-2016, 01:38 PM
  2. Solver VBA Loop
    By excelnoobie164 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-13-2014, 12:15 PM
  3. Solver Help: Setting a Constraint to Allow Two Values
    By Schooch47 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-23-2014, 09:05 PM
  4. Loop Solver with VBA
    By cloudwalking in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-20-2014, 03:04 PM
  5. Using solver with loop
    By mat1973 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2013, 02:01 PM
  6. Setting up Solver constraint
    By Jstewart304 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-23-2012, 01:36 PM
  7. Setting Solver Reference-programmatically
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-01-2006, 08:15 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