大家好:我对VBA来说是全新的,所以我使用宏录制器来帮助获得我想要的代码的要点。 我对Solver的所有输入都在一行中(例如我工作表的第19行)。我的目标单元格在列R中,决策单元格在列E和F中,约束在列G中。我最终想要创建更多输入行,比如10,在R列等中有10个目标函数... 我希望保持列的固定,但创建一个循环遍历10行的循环,为每行运行求解器,然后重置并运行下一行。当它到达第10行时,它会检查$ R29是否为""如果是这样它就会停止运行。
我知道我可能应该使用某种形式的" Do While Cells(Row,1)<> "" " contruction,但我迷失了如何在VBA中创建这个简单的循环。 谢谢
Sub RR_SC_OPTIMIZER()
'
' RR_SC_OPTIMIZER Macro
'
'
SolverOk SetCell:="$R$19", MaxMinVal:=2, ValueOf:=0, ByChange:="$E$19:$F$19", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$E$19", Relation:=1, FormulaText:="$G$19"
SolverOk SetCell:="$R$19", MaxMinVal:=2, ValueOf:=0, ByChange:="$E$19:$F$19", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOk SetCell:="$R$19", MaxMinVal:=2, ValueOf:=0, ByChange:="$E$19:$F$19", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
SolverOk SetCell:="$R$19", MaxMinVal:=2, ValueOf:=0, ByChange:="$E$19:$F$19", _
Engine:=1, EngineDesc:="GRG Nonlinear"
End Sub
答案 0 :(得分:0)
您可以尝试以下脚本(未经测试):
Sub RR_SC_OPTIMIZER()
Dim rngObjectCells As Range
Set rngObjectCells = Range("R19:R28")
Dim rngObjectCell As Range
For Each rngObjectCell In rngObjectCells
SolverReset
SolverOk SetCell:=rngObjectCell.Address, MaxMinVal:=2, ValueOf:=0, ByChange:=rngObjectCell.Offset(0, -13).Range("A1:B1").Address, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:=rngObjectCell.Offset(0, -13).Address, Relation:=1, FormulaText:=rngObjectCell.Offset(0, -11).Address
SolverOk SetCell:=rngObjectCell.Address, MaxMinVal:=2, ValueOf:=0, ByChange:=rngObjectCell.Offset(0, -13).Range("A1:B1").Address, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOk SetCell:=rngObjectCell.Address, MaxMinVal:=2, ValueOf:=0, ByChange:=rngObjectCell.Offset(0, -13).Range("A1:B1").Address, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
SolverOk SetCell:=rngObjectCell.Address, MaxMinVal:=2, ValueOf:=0, ByChange:=rngObjectCell.Offset(0, -13).Range("A1:B1").Address, _
Engine:=1, EngineDesc:="GRG Nonlinear"
Next
End Sub
基本上,你循环遍历你的硬编码范围R19:R28
(不是R29
,因为这会使它成为11行)并且在列R中的每个值都运行一个求解器。解算器中的所有引用现在都基于您循环的单元格。希望这符合您的目的。的问候,