我一直在尝试运行带有 Solver 问题的 VBA 代码,而无需手动勾选 References
中的框。 Tools
的部分VB 编辑器上的类别。为此,我必须添加 Application.Run
到我有求解函数的行。以下是我手动运行宏时的代码:
SolverOK SetCell:=Cells(LR + 1, 10), MaxMinVal:=3, ValueOf:="0", ByChange:="$N$4:$Q$4"
SolverSolve True
这已转换为:
Application.Run "SolverOK", "Cells(LR + 1, 10)", 3, "0", "$N$4:$Q$4"
Application.Run "SolverOptions", 0, 0, 0.000001, 0.0001, False, False, False, 1
Application.Run "SolverOptions", 100, 0, 0.075, False, True, 0, 0, 1, False, 30
Application.Run "SolverSolve", True
但是代码仍然缺少
AssumeNonNeg
出于某种原因的参数。我希望解决方案包含负数并使其等于 False
.没有骰子 ...这是我手动录制的宏:
SolverOk SetCell:="$J$59", MaxMinVal:=3, ValueOf:=0, ByChange:="$N$4:$Q$4", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOptions MaxTime:=0, Iterations:=0, Precision:=0.000001, Convergence:= _
0.0001, StepThru:=False, Scaling:=True, AssumeNonNeg:=True, Derivatives:=1
SolverOptions PopulationSize:=100, RandomSeed:=0, MutationRate:=0.075, Multistart _
:=False, RequireBounds:=True, MaxSubproblems:=0, MaxIntegerSols:=0, _
IntTolerance:=1, SolveWithout:=False, MaxTimeNoImp:=30
SolverSolve
无法弄清楚我到底做错了什么。谢谢你看我的问题。
我有 Windows 10、MS Office 2013,文件是 MS Excel 97-2003。
更新:
Application.Run "SolverOK", "Cells(LR + 1, 10)", 3, "0", "$N$4:$Q$4", 1, "GRG Nonlinear"
Application.Run "SolverOptions", 0, 0, 0.000001, False, False, 1, 1, 1, 1, True, 0.0001, False, 100, 0, False, True, 0.075, 0, 0, False, 30
Application.Run "SolverSolve", True
更新 2:
尝试完全复制@Peh 的代码。它确实适用于单个文件。但是,当我通过 shell(使用 VBS)为其他类似文件运行它时,我得到
Run-time error 1004: cannot run the macro 'solverOK'
.在网上搜索得到以下链接——VBA: Runtime error 1004 using Solver包括
Application.Run "SolverReset", True
一开始,得到了同样的错误Run-time error 1004: cannot run the macro 'solverReset. The macro may not be available in this workbook...'
最佳答案
你的问题可能是
SolverOptions MaxTime:=0, Iterations:=0, Precision:=0.000001, Convergence:= _
0.0001, StepThru:=False, Scaling:=True, AssumeNonNeg:=True, Derivatives:=1
SolverOptions PopulationSize:=100, RandomSeed:=0, MutationRate:=0.075, Multistart _
:=False, RequireBounds:=True, MaxSubproblems:=0, MaxIntegerSols:=0, _
IntTolerance:=1, SolveWithout:=False, MaxTimeNoImp:=30
不一样
Application.Run "SolverOptions", 0, 0, 0.000001, 0.0001, False, False, False, 1
Application.Run "SolverOptions", 100, 0, 0.075, False, True, 0, 0, 1, False, 30
为什么会这样?
在第一种情况下,您可以通过名称指定每个参数(例如
MaxTime:=0
)。因此,这些参数的顺序无关紧要。但是在第二种情况下,您不能按名称指定参数,因此您必须按顺序指定它们。因此(根据 SolverOptions Function 的文档)参数的顺序总是相同的
MaxTime, Iterations, Precision, …
.如果您现在调用SolverOptions
函数两次,您使用相同的参数但不同的值调用它们。在这里您需要在 中提交所有参数(按正确的顺序)一单函数调用:
SolverOptions(MaxTime, Iterations, Precision, AssumeLinear, StepThru, Estimates, Derivatives, SearchOption, IntTolerance, Scaling, Convergence, AssumeNonNeg, PopulationSize, RandomSeed, MultiStart, RequireBounds, MutationRate, MaxSubproblems, MaxIntegerSols, SolveWithout, MaxTimeNoImp)
在你的情况下
Application.Run "SolverOptions", MaxTime, Iterations, Precision, AssumeLinear, StepThru, Estimates, Derivatives, SearchOption, IntTolerance, Scaling, Convergence, AssumeNonNeg, PopulationSize, RandomSeed, MultiStart, RequireBounds, MutationRate, MaxSubproblems, MaxIntegerSols, SolveWithout, MaxTimeNoImp
'instead of the parameter names put in your values:
Application.Run "SolverOptions", 0, 0, 0.000001, False, False, 1, 1, 1, 1, True, 0.0001, True, 100, 0, False, True, 0.075, 0, 0, False, 30
'I replaced the missing parameter values with their default due to the documentation.
'Note that the values are re-ordered due to the correct parameter order
您需要为 设置一个值每此列表中的参数,无论是否需要,都不能省略,否则顺序会困惑。
SolverOK
中的相同问题SolverOk SetCell:="$J$59", MaxMinVal:=3, ValueOf:=0, ByChange:="$N$4:$Q$4", _
Engine:=1, EngineDesc:="GRG Nonlinear"
不一样
Application.Run "SolverOK", "Cells(LR + 1, 10)", 3, "0", "$N$4:$Q$4"
尤其是当你包围
"Cells(LR + 1, 10)"
时通过 ""
它是一个字符串,不会像预期的那样被评估为 VBA 代码。根据 SolverOk Function 的文档参数的顺序是
SolverOk(SetCell, MaxMinVal, ValueOf, ByChange, Engine, EngineDesc)
这应该会导致类似的结果:
Application.Run "SolverOK", Cells(LR + 1, 10), 3, 0, "$N$4:$Q$4", 1, "GRG Nonlinear"
关于vba - SolverOptions : How to find the parameter position (AssumeNonNeg)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48535436/