Excel VBA:编译错误:未定义函数子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21915646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:49:08  来源:igfitidea点击:

Excel VBA: Compile Error: Sub of Function not Defined

excelvbaexcel-vba

提问by BlueSun3k1

I'm trying to solve a problem with the following code but I just can't find a way around it. Whenever I try the Macro, I get an error "Sub or Function not defined". and the debugger highlights in yellow "Sub Solver_Overtime() and highlights in blue "SolverRest"

我正在尝试使用以下代码解决问题,但我找不到解决方法。每当我尝试宏时,都会收到错误“未定义子或函数”。调试器以黄色高亮显示“Sub Solver_Overtime()”并以蓝色高亮显示“SolverRest”

Sub Solver_OverTime()
Application.ScreenUpdating = False
Sheets("OverTime").Activate
SolverReset
SolverOptions MaxTime:=100, _
    Iterations:=100, _
    Precision:=0.000001, _
    AssumeLinear:=True, _
    StepThru:=False, _
    Estimates:=1, _
    Derivatives:=1, _
    SearchOption:=1, _
    IntTolerance:=5, _
    Scaling:=False, _
    Convergence:=0.0001, _
    AssumeNonNeg:=True
SolverAdd CellRef:="NET", Relation:=3, FormulaText:="NET_LIMIT"
SolverAdd CellRef:="shftCount", Relation:=1, FormulaText:="shftCountLimit"
SolverAdd CellRef:="schTemplate", Relation:=4, FormulaText:="integer"

SolverOk setcell:=Sheets("OverTime").Range("Intervals[[#Totals],[OT]]"), MaxMinVal:=2, ValueOf:="0", ByChange:=Sheets("OverTime").Range("Template_Schedule[COUNT]")
'SolverOk SetCell:="TargetCell", MaxMinVal:=2, ValueOf:="0", ByChange:="schTemplate"
SolverSolve True
End Sub

I'm not certain what the problem is because it runs just fine on another computer.

我不确定是什么问题,因为它在另一台计算机上运行得很好。

Thanks in advance.

提前致谢。

采纳答案by BlueSun3k1

I followed up on Tim William's instructions to add a Reference to the Solver Library by going to Tools > References > Solver. It did the job and it works as intended.

我按照 Tim William 的指示,通过转到“工具”>“参考”>“求解器”来添加对求解器库的引用。它完成了工作,并且按预期工作。