如何修改我录制的求解器宏以应用于范围?

时间:2017-05-11 14:53:42

标签: excel vba excel-vba solver

我正在尝试修改我在一系列单元格上运行Solver的下面脚本。我想要实现的差异:

  • 同时解决E行(不仅仅是C)
  • 添加约束
    • 行C乘以72%应等于行G
    • 行E次28%应等于行G
  • 对工作表中的所有行运行此操作
Sub MacroSolve()
    Worksheets("$100 All-In").Activate
    RowCount = 3
    Do While Not IsEmpty(Worksheets("$100 All-In").Range("A" & RowCount))
        SolverReset
        SolverOptions precision:=0.000001
        SolverOk SetCell:=Range("K" & RowCount), _
          MaxMinVal:=3, _
          ValueOf:="100", _
          ByChange:=Range("C" & RowCount)
        SolverSolve userFinish:=True
        SolverFinish keepFinal:=1
        RowCount = RowCount + 1
    Loop
    MsgBox "processing over" 
 End Sub

我录制了这个宏,显示了我在第一行的Solver中所做的事情,但我想对所有单元格进行此操作。

    SolverOk SetCell:="$K$3", MaxMinVal:=3, ValueOf:=125#, ByChange:="$C$3,$E$3" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$C$3", Relation:=2, FormulaText:="$P$3*$G$3"
    SolverAdd CellRef:="$E$3", Relation:=2, FormulaText:="$P$4*$G$3"
    SolverOk SetCell:="$K$3", MaxMinVal:=3, ValueOf:=125#, ByChange:="$C$3,$E$3" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$K$3", MaxMinVal:=3, ValueOf:=125#, ByChange:="$C$3,$E$3" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve
    SolverOk SetCell:="$K$3", MaxMinVal:=3, ValueOf:=125#, ByChange:="$C$3,$E$3" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"
End Sub

0 个答案:

没有答案