EXCEL VBA Run Solver Over Multiple rows Using a Loop -
hi all: brand new vba, used macro recorder gist of code want. of inputs solver in 1 row (e.g. row 19 of worksheet). objective cell in column r, decision cells in columns e , f , constraint in column g. want create more input rows, 10, 10 objective functions in column r etc... want keep columns fixed create loop loops on 10 rows, running solver each row , resets , runs next row. when reaches row 10 checks see if $r29 "" if stops running.
i know should use form of "do while cells(row,1) <> "" " contruction, @ losts how create simple loop in vba. thank you
sub rr_sc_optimizer() ' ' rr_sc_optimizer macro ' ' solverok setcell:="$r$19", maxminval:=2, valueof:=0, bychange:="$e$19:$f$19", _ engine:=1, enginedesc:="grg nonlinear" solveradd cellref:="$e$19", relation:=1, formulatext:="$g$19" solverok setcell:="$r$19", maxminval:=2, valueof:=0, bychange:="$e$19:$f$19", _ engine:=1, enginedesc:="grg nonlinear" solverok setcell:="$r$19", maxminval:=2, valueof:=0, bychange:="$e$19:$f$19", _ engine:=1, enginedesc:="grg nonlinear" solversolve solverok setcell:="$r$19", maxminval:=2, valueof:=0, bychange:="$e$19:$f$19", _ engine:=1, enginedesc:="grg nonlinear" end sub
you can try following script (untested):
sub rr_sc_optimizer() dim rngobjectcells range set rngobjectcells = range("r19:r28") dim rngobjectcell range each rngobjectcell in rngobjectcells solverreset solverok setcell:=rngobjectcell.address, maxminval:=2, valueof:=0, bychange:=rngobjectcell.offset(0, -13).range("a1:b1").address, _ engine:=1, enginedesc:="grg nonlinear" solveradd cellref:=rngobjectcell.offset(0, -13).address, relation:=1, formulatext:=rngobjectcell.offset(0, -11).address solverok setcell:=rngobjectcell.address, maxminval:=2, valueof:=0, bychange:=rngobjectcell.offset(0, -13).range("a1:b1").address, _ engine:=1, enginedesc:="grg nonlinear" solverok setcell:=rngobjectcell.address, maxminval:=2, valueof:=0, bychange:=rngobjectcell.offset(0, -13).range("a1:b1").address, _ engine:=1, enginedesc:="grg nonlinear" solversolve solverok setcell:=rngobjectcell.address, maxminval:=2, valueof:=0, bychange:=rngobjectcell.offset(0, -13).range("a1:b1").address, _ engine:=1, enginedesc:="grg nonlinear" next end sub
basically looping through hardcoded range r19:r28
(not r29
make 11 rows) , each value in column r running solver. references in solver based on cell looping through. hope serves purpose. regards,
Comments
Post a Comment