我需要在工作表中多次使用Excel Solver。我需要使用求解器的每个场景具有相同的格式,因此我想使用带有相对引用的宏来允许我运行求解器而不会每次都设置问题。我有以下代码,但SolverOk函数的ByChange参数给了我一个错误。
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
我在各种网站上看到了很多类似的例子,但是他们都使用绝对引用,至少对于ByChange参数。是否有正确的方法来使ByChange参数接受相对引用?
答案 0 :(得分:0)
“我想使用带有相对引用的宏来允许我运行求解器而不必每次都设置问题” - 您不需要仅为此目的使用相对引用。这段代码是由您录制还是打孔?