+ Reply to Thread
Results 1 to 11 of 11

Solver VBA Loop

  1. #1
    Registered User
    Join Date
    12-12-2014
    Location
    San Diego
    MS-Off Ver
    2010 excel
    Posts
    6

    Solver VBA Loop

    Hello, I am new to VBA and I am trying to create a Solver macro that loops and solves for each row until the end. My Solver code is below.



    Sub ASPs()

    SolverReset
    SolverOK MaxMinVal:=0, ValueOf:=0, ByChange:="$AR$5:$AS$5", Engine:=1, _
    EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$AW$5", Relation:=2, FormulaText:="$AT$5"
    SolverOK MaxMinVal:=0, ValueOf:=0, ByChange:="$AR$5:$AS$5", Engine:=1, _
    EngineDesc:="GRG Nonlinear"
    SolverOK MaxMinVal:=0, ValueOf:=0, ByChange:="$AR$5:$AS$5", Engine:=1, _
    EngineDesc:="GRG Nonlinear"
    SolverSolve UserFinish:=True
    End Sub




    One of the solutions I have seen is the one below but I can't get the FormulaText to continue in the Loop.


    Sub ASPLoop()


    Dim cellChange As Range
    Dim cellGoal As Range
    Dim cellConstraint As Range

    Set cellChange = ActiveSheet.Range("AR5:AS5")
    Set cellGoal = ActiveSheet.Range("AW5")
    Set cellConstraint = ActiveSheet.Range("AT5")

    Do '********* LOOP & SOLVE ***************
    SolverReset
    SolverOptions Precision:=0.001
    SolverOK SetCell:=cellGoal.Address(True, True), _
    MaxMinVal:=1, ByChange:=cellChange.Address(True, True)
    SolverAdd CellRef:=cellConstraint.Address(True, True), _
    Relation:=2, FormulaText:="AT5"
    Solver.SolverSolve UserFinish:=True

    Set cellChange = cellChange.Offset(1, 0)
    Set cellGoal = cellGoal.Offset(1, 0)
    Set cellConstraint = cellConstraint.Offset(1, 0)

    Loop While Trim(cellGoal.Text) <> "" 'until goal cell is empty
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Solver VBA Loop

    Please post a workbook.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    12-12-2014
    Location
    San Diego
    MS-Off Ver
    2010 excel
    Posts
    6

    Re: Solver VBA Loop

    I have attached my workbook and when you press the Macro button that I assigned the Macro to, it fills out what I need to columns AR and AS, but it only does for the first row. I am trying to make it so it loops and does it for all rows.

    Thank you,
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    12-12-2014
    Location
    San Diego
    MS-Off Ver
    2010 excel
    Posts
    6

    Re: Solver VBA Loop

    I have attached my workbook and when you press the Macro button that I assigned the Macro to, it fills out what I need to columns AR and AS, but it only does for the first row. I am trying to make it so it loops and does it for all rows.

    Thank you,

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Solver VBA Loop

    The problem is under-defined -- you can set the cost for either procedure to any random value and change the other to achieve the total. For example,

    Row\Col
    AP
    AQ
    AR
    AS
    AT
    AU
    AV
    AW
    1
    Cervical Cases Lumbar cases Cervical ASP Lumbar ASP Tot Rev Cerv Rev Lumb Rev Tot Rev
    2
    179
    222
    0
    26,745
    5,933,925
    0
    5,933,925
    5,933,925
    Last edited by shg; 12-12-2014 at 07:44 PM.

  6. #6
    Registered User
    Join Date
    12-12-2014
    Location
    San Diego
    MS-Off Ver
    2010 excel
    Posts
    6

    Re: Solver VBA Loop

    I am not sure what you mean by chaning to any random value. The number of procedures for cervical and lumbar is given as well as the total for each row. The Solver would work for each row if I used it for each row. Is there not a way to make a simple loop to make it solve for each row?

    Thank you for your help.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Solver VBA Loop

    Am I missing something? Here are three possible solutions for the first row. Which is correct?

    Row\Col
    A
    B
    C
    D
    E
    AP
    AQ
    AR
    AS
    AT
    AU
    AV
    AW
    1
    State State Full CountyName MktName(for_SAP) MSA Cervical Cases Lumbar cases Cervical ASP Lumbar ASP Tot Rev Cerv Rev Lumb Rev Tot Rev
    2
    AK ALASKA Anchorage Borough AK Anchorage, AK
    179
    222
    0
    26,745
    5,933,925
    0
    5,933,925
    5,933,925
    3
    AK ALASKA Anchorage Borough AK Anchorage, AK
    179
    222
    33,189
    0
    5,933,925
    5,933,925
    0
    5,933,925
    4
    AK ALASKA Anchorage Borough AK Anchorage, AK
    179
    222
    14,810
    14,810
    5,933,925
    2,648,001
    3,285,924
    5,933,925

  8. #8
    Registered User
    Join Date
    12-12-2014
    Location
    San Diego
    MS-Off Ver
    2010 excel
    Posts
    6

    Re: Solver VBA Loop

    I see your point with there being 3 options. I think that maybe Columns AU and AV can be ignored. I didn't understand that because if there were cervical cases performed, there wouldn't be zero revenue earned. The only place there would be zero revenue is where there are zero cases performed. I was using the formula of
    X(# of Cervical Cases)+Y(# of Lumbar Cases)=Total Revenue.
    Where X=Cervical ASP and Y=Lumbar ASP. Is there a way to Loop is the way I did with the Solver macro if we ignore the Cervical revenue(column AU) and Lumbar revenue(Column AV) in order to focus on only the total revenue so there is only one option?

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Solver VBA Loop

    Those two columns are irrelevant to the calculation. You have one equation and two unknowns; that's not enough information:

    X*C + Y*L =T (right?)

    So X = (T - Y*L) / C

    Substitute ANY value for Y and you can solve for X.
    Last edited by shg; 12-13-2014 at 01:14 PM.

  10. #10
    Registered User
    Join Date
    12-12-2014
    Location
    San Diego
    MS-Off Ver
    2010 excel
    Posts
    6

    Re: Solver VBA Loop

    Thank you for clearing that up. Do you know why when I use the Solver, it returned 13,067 for my X and 16,215 for my Y? Is that because it auto-defaults to dividing the constraint in half making X and Y as close to each other as possible?

    Thank you for your time and explaination.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Solver VBA Loop

    No idea .

+ 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] Using Solver in a Loop
    By mattian31 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-02-2016, 02:17 AM
  2. Using solver with loop
    By mat1973 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2013, 02:01 PM
  3. VBA Loop with Solver.
    By ec41tp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-17-2011, 11:23 AM
  4. VBA Loop using solver.
    By ec41tp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2011, 05:10 AM
  5. Solver Loop
    By Boof in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-08-2011, 01:28 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