PDA

View Full Version : Help with macro for solver using variable cells



DeucesAx
10-31-2015, 06:41 PM
Let’s say I want to use a macro to use solver inmultiple rows with the same relative parameters. The value in column eshould be changed in a way to make the values in columns e and f equal andf needs the greater than 0.00001. I am fine with clicking on the cell in columnf in the appropriate row.

My code seems to work, besides the "equal" constraint, where excelseems to assign the original value, instead of iterating to the correct solution.
Here is my code:


Sub solvr()
'
' solver Macro
'
' Keyboard Shortcut: Ctrl+s
'
Dim fr As Integer
Dim fc As Integer
Dim rc As Integer
Dim lc As Integer

fr = ActiveCell.Row
fc = ActiveCell.Column
rc = fc + 1
lc = fc + 2
SolverReset
SolverOk SetCell:=Cells(fr, rc), MaxMinVal:=1, ValueOf:=0, ByChange:=Cells(fr, fc), Engine _
:=1, EngineDesc:="GRG Nonlinear"


SolverAdd CellRef:=Cells(fr, rc), Relation:=2, FormulaText:=Cells(fr, lc)
SolverAdd CellRef:=Cells(fr, fc), Relation:=3, FormulaText:="0.000001"

SolverSolve
End Sub


I can not for the life of e find out what is wrong.