+ Reply to Thread
Results 1 to 9 of 9

How to increment cell references in solver macro loop

  1. #1
    Registered User
    Join Date
    02-01-2019
    Location
    Wahington
    MS-Off Ver
    2016
    Posts
    5

    How to increment cell references in solver macro loop

    I have a worksheet the calculates pricing based on a set margin and npv criteria. Everything I'm referring to is in one sheet. Price input table is in a range of j3:k19. The table reflects pricing by term and product options. Term years 1-7 across columns and product A-X across rows. Tied to that pricing is a financial calculator and two associated data tables. One table for margin outcomes and one for npv outcomes. The range sizes on the price input and data tables are the same. The ordering of the pricing and criteria tables is the same. Objective is to create a solver macro that will solve for the pricing in j3:k19 subject to constraints in the margin and npv data tables. Here is what the solver code looks like for computing the price in cell j3:
    SolverReset
    SolverOk SetCell:="$K$24", MaxMinVal:=3, ValueOf:=0.2, ByChange:="$J$3", Engine _
    :=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$B$24", Relation:=3, FormulaText:="0.24"
    SolverSolve UserFinish:=True

    I want code that will run that same solver code criteria for each cell in the price input range without having to write the same code for every cell with updated cell references in the input and criteria ranges. I know how to set up easy loop macros, but in this example, I don't know how to move the cell references in tandem across the price input and margin and npv criteria tables. So my questions are what is the best way to wrap a loop around this and have the solver calc references move in tandem with the price range reference. Something like this:

    Sub Pricing ()
    Dim cell As Range
    For Each cell In ActiveSheet.Range("j3:k19")
    Enter dynamic solver code here
    Next cell
    End Sub

    Appreciate any input I can get. Thanks

  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: How to increment cell references in solver macro loop

    I'm not sure I understand you description of your problem. You wish to run solver on each cell in the range J3:K19 but in your uploaded example the cell to change is J3 and the target cell is K24.

    Assuming that cells to change is found in the J column and target cell should be in the K column then something like this could be used for the loop:

    Please Login or Register  to view this content.
    and if the range for cells to change are J:K you can define this as

    Please Login or Register  to view this content.
    Alf

  3. #3
    Registered User
    Join Date
    02-01-2019
    Location
    Wahington
    MS-Off Ver
    2016
    Posts
    5

    Re: How to increment cell references in solver macro loop

    Thanks for helping Alf. I apologize for making a mistake on my range reference. The pricing input table range is actually J3:P19. The IRR table range is K24:Q41 and the margin table range is B24:H41 My VBA skill set is not good, so I may not fully understand how your proposed solution will work. I will play with it tomorrow. I think the the key missing piece here might be having the code increment all of the solver criteria in tandem with the pricing table input referenced cell. Maybe an example image of what I have will help. The actual tables look very similar to this example. So if we pretend the first price input cell of the example pricing input table is cell J3, I am wanting a macro that will solve for the price in J3 that results in an irr of 20% in the irr table or possibly higher, in the event the >= 24% margin constraint in the solver is tripped. I then want the macro to move to cell K3 and do the same thing, but in addition to the move to K3, the solver cell references for irr and margin also have to move/increment to make sure it is referencing the irr and margin associated with the price refernce. Once it gets to cell P3 on the first row of the range, drop to next row and on and on until the end of the range. The actual order of cell movement does not matter as long as every cell in the price input table gets solved. Does this description make more sense? Example.png
    Last edited by keros; 02-03-2019 at 02:17 PM.

  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: How to increment cell references in solver macro loop

    Can't see how to set up solver as the correlation between solver an the different tables are not clear to me, can you upload a file shoving what walues shall be changed and how
    the result should be?

    I've uploaded a small solver model that runs in a loop. There are also some comments in the file to show how the loop is set up. Perhaps this will be of help
    to you.

    Alf
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-01-2019
    Location
    Wahington
    MS-Off Ver
    2016
    Posts
    5

    Re: How to increment cell references in solver macro loop

    Thanks Alf. I will take a look at this today and see if I can incorporate it into what I'm trying to accomplish. I've attempted to include an attachment this time just in case it gives
    you further solution inspiration. Hopefully it helps better describe the objective.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    02-01-2019
    Location
    Wahington
    MS-Off Ver
    2016
    Posts
    5

    Re: How to increment cell references in solver macro loop

    I tried using your example exercise. In order to use it, I think I will need to align all my tables into the same column or I end up with code that looks like this trying to mimic your example:
    Sub Pricing()
    '
    ' Pricing Macro
    '
    '
    Dim p As Integer
    Dim i As Integer
    Dim d As Integer
    For p = 10 To 16 'for price table increment reference
    For i = 11 To 17 'for irr table increment reference
    For d = 2 To 8 'for dm table increment reference

    SolverReset
    SolverOk SetCell:="(24,i)", MaxMinVal:=3, ValueOf:=0.2, ByChange:="(3,p):", Engine _
    :=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="(24,d)", Relation:=3, FormulaText:="0.24"
    SolverSolve UserFinish:=True

    ' I don 't know how to NEXT when I have multiple "for" statements.

    End Sub

  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: How to increment cell references in solver macro loop

    I don 't know how to NEXT when I have multiple "for" statements.
    Since you have 3 "For" statement you need 3 "Next" and the way it will work is that the last "For" will be "solved" first.

    So first p = 10, i = 11 and d = 2 and with this setup solver will do the first run, the second run will use the same p and i value but d will now be 3. For the 3 run d = 4 and p and i are still 10 and 11 and so forth until d = 8.

    The next run after d = 8 the i value will change to 12 and the d value will start at 2 again loop through the d values 2 to 8, then change the i value to 13 and loop through all d values and so fort.

    When the i = 17 the next loop change the p value to 11.

    Please Login or Register  to view this content.
    Solver will not understand SetCell:="(24,i)" so I've changed your code a bit. When writing code it should be wrapped click on the "#" icon and write your code between two [CODE] symbols you get.

    In you code you wrote Engine:=1, EngineDesc:="GRG Nonlinear" this is not wrong but you only need to specify either "Engine:=1" or EngineDesc:="GRG Nonlinear"

    Alf
    Last edited by Alf; 02-04-2019 at 06:53 PM.

  8. #8
    Registered User
    Join Date
    02-01-2019
    Location
    Wahington
    MS-Off Ver
    2016
    Posts
    5

    Re: How to increment cell references in solver macro loop

    Thanks for your help Alf.

  9. #9
    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: How to increment cell references in solver macro loop

    You are welcome and hopefully you now have sufficient information for the solver setup you need. If not don't hesitate to post again

    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. Macro for Solver with a loop and change variable cell capability
    By CHRISTINEKENDALL93 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2018, 10:48 AM
  2. [SOLVED] Increment Cell references inside of an IF Formula
    By Feicstur in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-07-2015, 01:43 PM
  3. [SOLVED] increment all cell references in an array formula by 24 when copied down a row
    By ariapro22 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-29-2013, 05:51 AM
  4. Increment columns, cell references, and labels.
    By XLVBA in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2012, 01:59 PM
  5. Increment Cell References
    By Djokovic in forum Excel General
    Replies: 2
    Last Post: 03-11-2011, 01:03 PM
  6. Replies: 2
    Last Post: 07-05-2006, 02:50 PM
  7. [SOLVED] [SOLVED] How to auto-increment data source cell references when copying cha
    By Dave in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 06-28-2006, 12:45 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