如何在VBA中自动化求解器?

时间:2019-05-17 04:46:36

标签: excel vba solver

我想使用可变范围和数据来自动执行excel解算器。制定了以下代码,但无法解决

Sub excel_solver()
'
' excel_solver Macro
'

'
    SolverReset
    SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:=0, ByChange:=ActiveCell.Offset(0, -19).Range(Selection, Selection.End(xlDown)).Select, Engine:=2, EngineDesc:="Simplex LP"
    SolverAdd CellRef:=ActiveCell.Offset(0, -19).Range(Selection, Selection.End(xlDown)).Select, Relation:=5, FormulaText:="binary"
    SolverAdd CellRef:=ActiveCell.Offset(0, 2).Range(Selection, Selection.End(xlDown)).Select, Relation:=2, FormulaText:="1"
    SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:=0, ByChange:=ActiveCell.Offset(0, -19).Range(Selection, Selection.End(xlDown)).Select, Engine:=2, EngineDesc:="Simplex LP"
    SolverOk SetCell:="$AC$29542", MaxMinVal:=2, ValueOf:=0, ByChange:=ActiveCell.Offset(0, -19).Range(Selection, Selection.End(xlDown)).Select, Engine:=2, EngineDesc:="Simplex LP"
    SolverSolve

End Sub

1 个答案:

答案 0 :(得分:0)

This worked-
SolverReset
    SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:=0, ByChange:= _
        Range(ActiveCell.Offset(0, -19), ActiveCell.Offset(0, -19).End(xlDown)), Engine:=2, EngineDesc:="Simplex LP"
    SolverAdd CellRef:=Range(ActiveCell.Offset(0, 2), ActiveCell.Offset(0, 2).End(xlDown)), Relation:=2, FormulaText:="1"
    SolverAdd CellRef:=Range(ActiveCell.Offset(0, -19), ActiveCell.Offset(0, -19).End(xlDown)), Relation:=5, FormulaText:="binary"
    SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:=0, ByChange:= _
        Range(ActiveCell.Offset(0, -19), ActiveCell.Offset(0, -19).End(xlDown)), Engine:=2, EngineDesc:="Simplex LP"
    SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:=0, ByChange:= _
        Range(ActiveCell.Offset(0, -19), ActiveCell.Offset(0, -19).End(xlDown)), Engine:=2, EngineDesc:="Simplex LP"
    SolverSolve