【发布时间】:2016-03-14 19:56:37
【问题描述】:
所以这是我的问题。我正在尝试编写一个使用求解器执行功能的循环。基本上它从一个单元格开始,运行求解器,将目标单元格和约束单元格移动到右侧,求解,将目标单元格和约束单元格向右移动 14,求解,右侧,求解,右侧 14 求解,直到找到空白。
这是前几行代码,向您展示我在长版本中所做的事情。
Range("RI6").Select
SolverOk SetCell:="$RI$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RG$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$RI$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$RI$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("RJ16").Select
SolverOk SetCell:="$RJ$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RJ$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$RJ$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$RJ$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("RX16").Select
SolverOk SetCell:="$RX$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RX$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$RX$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$RX$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("RY16").Select
SolverOk SetCell:="$RY$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RY$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$RY$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$RY$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("SM16").Select
SolverOk SetCell:="$SM$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$SM$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$SM$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$SM$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("SN16").Select
SolverOk SetCell:="$SN$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$SN$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$SN$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$SN$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("TB16").Select
SolverOk SetCell:="$TB$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TB$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$TB$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$TB$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("TC16").Select
SolverOk SetCell:="$TC$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TC$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$TC$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$TC$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("TQ16").Select
SolverOk SetCell:="$TQ$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TQ$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$TQ$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$TQ$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("TR16").Select
SolverOk SetCell:="$TR$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TR$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$TR$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$TR$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("UF16").Select
SolverOk SetCell:="$UF$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$UF$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$UF$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$UF$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
Range("UG16").Select
SolverOk SetCell:="$UG$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$UG$1", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$UG$1", Relation:=3, FormulaText:="74"
SolverAdd CellRef:="$UG$1", Relation:=1, FormulaText:="87"
SolverSolve (True)
提前感谢您的任何帮助/建议。
【问题讨论】:
-
您似乎没有任何问题。你的代码真的有效吗?如果是这样 - 您可以使用循环来缩短它。
-
如果您在编写循环时需要帮助,那么您可能需要查看以下网站:homeandlearn.org/excel_vba_for_loops.html
-
我的问题是如何进行单元格移动,我试图循环它并且无法使其正常工作。该代码在写出很长的路时有效,但在我尝试循环时无效。