Excel Solver Error while using VBA

2019-07-24 20:25发布

I have the following code which is basically a macro recorded. after recording the macro, I cleaned away some lines of code that were found repetitive. When I try to run this code again I get a Sub or Function not defined error. I checked out other Solver VBA questions on Stackoverflow and I dont seemt to be doing anything wrong. Can anyone please help? Thanks in advance.

Sub Macro2()

SolverOk SetCell:="$BF$7", MaxMinVal:=2, ValueOf:=0, ByChange:="$BA$7:$BC$7", Engine:=2, EngineDesc:="Simplex LP"

SolverAdd CellRef:="$BA$7", Relation:=4, FormulaText:="integer"
SolverAdd CellRef:="$BA$7", Relation:=3, FormulaText:="0"

SolverAdd CellRef:="$BB$7", Relation:=4, FormulaText:="integer"
SolverAdd CellRef:="$BB$7", Relation:=3, FormulaText:="0"

SolverAdd CellRef:="$BC$7", Relation:=3, FormulaText:="0"
SolverAdd CellRef:="$BC$7", Relation:=4, FormulaText:="integer"

SolverAdd CellRef:="$BE$7", Relation:=3, FormulaText:="0"

SolverOk SetCell:="$BF$7", MaxMinVal:=2, ValueOf:=0, ByChange:="$BA$7:$BC$7", Engine:=2, EngineDesc:="Simplex LP"

SolverSolve
End Sub

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-24 20:58

The Solver is an add in, so it won't work in your vba code until you enable it. I use 07, so you click office button -> options -> add-ins -> manage add ins to enable it. Not sure about other versions.

Once you have enabled the add-in, you must then connect the add-in to the vba editor. Open the VBA Editor (Alt+F11), then go to Tools -> References, then check off "SOLVER" and hit OK.

see this MSDN

http://msdn.microsoft.com/en-us/library/office/ff196600%28v=office.15%29.aspx

查看更多
登录 后发表回答