+ Reply to Thread
Results 1 to 5 of 5

Dynamically find variable cells range in excel solver

  1. #1
    Registered User
    Join Date
    02-07-2008
    Posts
    3

    Dynamically find variable cells range in excel solver

    Hi,
    I have the below VBA code, but I get an error when I pass the varRange variable to the range in SolverOk. My goal is to have the Variable Cells and the Constraints in Solver to have dynamic range.



    Sub Solver3()

    Dim lastRow As Long
    Dim varRange As String

    With ThisWorkbook.Sheets("Sheet11")
    lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    varRange = "D18:D" & lastRow

    SolverReset

    SolverOk SetCell:="$C$31", MaxMinVal:=2, ValueOf:=0, varRange, _
    Engine:=3, EngineDesc:="Evolutionary"
    SolverAdd CellRef:=varRange, Relation:=6, FormulaText:="AllDifferent"
    SolverAdd CellRef:="$G$18", Relation:=2, FormulaText:="1"
    SolverOk SetCell:="$C$31", MaxMinVal:=2, ValueOf:=0, varRange, _
    Engine:=3, EngineDesc:="Evolutionary"
    SolverOk SetCell:="$C$31", MaxMinVal:=2, ValueOf:=0, varRange, _
    Engine:=3, EngineDesc:="Evolutionary"
    SolverSolve userFinish:=True
    End With

    End Sub


    Thanks,

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2505
    Posts
    2,790

    Re: Dynamically find variable cells range in excel solver

    I have no experience coding solvers but in looking at some sample code on Microsoft I noticed that the ranges are coded differently than the way you show.
    SolverAdd CellRef:=Range("C4:E6")
    So I have no idea if it matters or not but I thought I would put this out there.
    Good luck!

  3. #3
    Forum Contributor
    Join Date
    01-25-2025
    Location
    Presov, Slovakia
    MS-Off Ver
    2021
    Posts
    279

    Re: Dynamically find variable cells range in excel solver

    Hi planner07,

    Please Login or Register  to view this content.
    m.s.

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

    Re: Dynamically find variable cells range in excel solver

    A recorded macro for a solver setup repeats it self so using MSDols tip to solve this problem I would think a bit shorter code should work as well.

    Please Login or Register  to view this content.
    the three solver engines can be added as "Engine:=1"or "Engine:=2" or "Engine:=3"

    Alf

  5. #5
    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,794

    Re: Dynamically find variable cells range in excel solver

    The max number of rows in an Excel spreadsheet is 1048578.

    Using a "dim" statement in VBA for the rows they could be dimed as "Long" or "Integer".
    Max number for "Long": 2147483648
    Max number for "Integer": 32767

    so it seems best to dim row number as Long since this exceeds the max number of rows in a spreadsheet. On the other hand the Excel Solver has an inbuild limit of about 200 constraints so if this limit is exceeded solver will stop with a message box "To many cells".

    In the uploaded macro range starts at D18 and I would guestimate that solver can handle a total range of D18:D210.

    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: 3
    Last Post: 05-11-2023, 10:15 AM
  2. Dynamic variable cells in Solver VBA macro
    By hasselberg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2021, 02:27 AM
  3. VBA for Dynamic range Solver generates error-Not variable cells but marked All Diff & Int
    By rohit.patra007 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-03-2021, 07:46 AM
  4. Replies: 9
    Last Post: 07-04-2020, 07:39 PM
  5. [SOLVED] Create a dynamic range for by changing variable cells box in solver
    By Ad896832 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-26-2017, 02:03 PM
  6. Replies: 4
    Last Post: 12-13-2015, 04:35 PM
  7. Replies: 1
    Last Post: 07-14-2011, 01:57 PM

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