I want to create VBA to run Solver with these parameters:
Set objective: G6
Minimise
By changing Variables cells: J6:J----
Subject to the constraints: L6:L-- = N6:N-- (for example: L6=N6 , L7=N7 , ...)
The problem is that the variables and constraints ranges varies where I typed ---- and --. I know that the Variables cells range goes until a number on cell N1 and the constraints cells range goes until a number on cell N3.
I don't know how to create VBA but I tried that (it's not working):
Sub SolverMacro()
Dim var
Dim var2
var = Range("Sheet2!$N$1").Value
var2 = Range("Sheet2!$N$3").Value
SolverReset
SolverAdd CellRef:=Range("L6", "L" & var2), Relation:=2, FormulaText:=Range("N1", "N" & var2)
SolverOptions Assumenonneg:=True
SolverOk SetCell:="$G$6", _
MaxMinVal:=2, _
ByChange:=Range("J6", "J" & var)
SolverSolve userFinish:=True
End Sub
How can I get this working?
Thanks everyone! I found an answer and it's working now. The code that works is:
Sub SolverMacroX()
SolverReset
Dim x As Integer
Dim Aux As Long
Aux = Range("$L$3")
For x = 6 To Aux
SolverAdd CellRef:="L" & x, Relation:=2, FormulaText:=Range("N" & x)
Next
SolverOptions AssumeNonNeg:=True
SolverOk SetCell:="$G$6", _
MaxMinVal:=2, _
ByChange:=Range("J6", "J" & Range("$J$3")), _
SolverSolve userFinish:=True
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.