title: VBA - Solver Macro with Relative References
body: <p>I need to use the Excel Solver several times in a worksheet. Each scenario that I need to utilize the solver for has the same formatting so I'd like to use a macro with relative references to allow me to run the solver without setting the problem up each time. I have the following code but the ByChange argument for the SolverOk function is giving me an error. </p>
  
  <pre><code>Sub solverMacro()
  
  SolverReset
  SolverOk SetCell:=ActiveCell.Offset(0, 0), MaxMinVal:=2, ValueOf:=0, ByChange:= _
          Range(ActiveCell.Offset(-3, 0), ActiveCell.Offset(-5, 0), ActiveCell.Offset(-7, 0)), _
          Engine:=1, EngineDesc:="GRG Nonlinear"
      SolverAdd CellRef:=ActiveCell.Offset(-7, 0), Relation:=3, FormulaText:="1"
      SolverAdd CellRef:=ActiveCell.Offset(-7, 0), Relation:=4, FormulaText:="integer"
      SolverAdd CellRef:=ActiveCell.Offset(-5, 0), Relation:=3, FormulaText:="1"
      SolverAdd CellRef:=ActiveCell.Offset(-5, 0), Relation:=4, FormulaText:="integer"
      SolverAdd CellRef:=ActiveCell.Offset(-3, 0), Relation:=3, FormulaText:="1"
      SolverAdd CellRef:=ActiveCell.Offset(-3, 0), Relation:=4, FormulaText:="integer"
      SolverAdd CellRef:=ActiveCell.Offset(3, 0), Relation:=3, FormulaText:=ActiveCell.Offset(-9, 0)
      SolverAdd CellRef:=ActiveCell.Offset(4, 0), Relation:=3, FormulaText:=ActiveCell.Offset(-9, 0)
      SolverAdd CellRef:=ActiveCell.Offset(5, 0), Relation:=3, FormulaText:=ActiveCell.Offset(-9, 0)
      SolverAdd CellRef:=ActiveCell.Offset(6, 0), Relation:=3, FormulaText:=ActiveCell.Offset(-9, 0)
      SolverOk SetCell:=ActiveCell.Offset(0, 0), MaxMinVal:=2, ValueOf:=0, ByChange:= _
          Range(ActiveCell.Offset(-3, 0), ActiveCell.Offset(-5, 0), ActiveCell.Offset(-7, 0)),     _
          Engine:=1, EngineDesc:="GRG Nonlinear"
      SolverOk SetCell:=ActiveCell.Offset(0, 0), MaxMinVal:=2, ValueOf:=0, ByChange:= _
          Range(ActiveCell.Offset(-3, 0), ActiveCell.Offset(-5, 0), ActiveCell.Offset(-7, 0)),     _
          Engine:=1, EngineDesc:="GRG Nonlinear"
      SolverSolve
  
  End Sub
  </code></pre>
  
  <p>I see a lot of similar examples on various websites but ALL of them use absolute references, at least for the ByChange argument. Is there a correct way to get the ByChange argument to accept relative references?</p>