I am writing VBA code that uses solver in excel to minimize error between reference values and values predicted by my equation. But, I would like to keep equations in the VBA code than adding them as columns in the excel. This is to secure my equations using a password protected macro. The codes goes like below:
Cells B2 and C2 are the variables which will be varied by solver to reach a minimum. Cells F9 and G9 are the reference values.
Range("B2").Select
ActiveCell.FormulaR1C1 = "1"
Range("C2").Select
ActiveCell.FormulaR1C1 = "1"
SolverReset
SolverAdd CellRef:="$B$2", Relation:=3, FormulaText:="0.3"
SolverAdd CellRef:="$B$2", Relation:=1, FormulaText:="1.7"
SolverAdd CellRef:="$C$2", Relation:=3, FormulaText:="1"
SolverAdd CellRef:="$C$2", Relation:=1, FormulaText:="20"
value1 = 28.563041 - 0.6895786 * Range("B2").Value - 0.1685979 *
Range("C2").Value
value2 = 31.161328 - 0.1194945 * Range("B2").Value - 0.1156287 *
Range("C2").Value
error1 = (Range("$F$9").Value - value1 ) ^ 2
error2 = (Range("$G$9").Value - value2) ^ 2
error = error1 + error2
Range("J11").Value = error
SolverOk SetCell:="$J$11", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$2:$C$2", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
Problem: The variables B2 and C2 are initialized with a value of 1 but are not changed during optimization by solver. I would like to know how to get variables in the loop of solver.