If necessary, please refer to "badSolver.xlsm" (click here) [1]. Ignore any preview errors; box.net limitations.
How can I make Application.Run "SolverOptions" work?
The following macros work just fine when I select the Solver reference (Tools > References). Execute goodTest.
Sub goodTest()
solveIt "F1", "F2"
End Sub
Private Sub solveIt(myChangeCell As String, mySetCell As String)
Dim x As Variant
Range(myChangeCell).ClearContents
SolverReset
SolverOk setCell:=mySetCell, MaxMinVal:=3, _
ValueOf:=0, ByChange:=myChangeCell, Engine:=1, _
EngineDesc:="GRG Nonlinear"
SolverOptions MaxTime:=0, Iterations:=0, _
Precision:=0.00000000001, Convergence:=0.00000000001, _
StepThru:=False, Scaling:=True, AssumeNonNeg:=True, _
Derivatives:=1
SolverOptions PopulationSize:=True, RandomSeed:=0, _
MutationRate:=0.075, Multistart:=False, _
RequireBounds:=True, MaxSubproblems:=0, _
MaxIntegerSols:=0, IntTolerance:=1, SolveWithout:=False, _
MaxTimeNoImp:=30
x = SolverSolve(True)
If x > 3 Then
With Range(myChangeCell)
.HorizontalAlignment = xlRight
.Value = "#Error " & x
End With
End If
End Sub
But the following macros, in another module, fail on the highlighted line with runtime error #13 ("type mismatch") when I deselect the Solver reference, as intended. Execute badTest.
Sub badTest()
solveIt "F1", "F2"
End Sub
Private Sub solveIt(myChangeCell As String, mySetCell As String)
Dim x As Variant
Range(myChangeCell).ClearContents
Application.Run "SolverReset"
Application.Run "SolverOk", "setCell:=mySetCell", "MaxMinVal:=3", _
"ValueOf:=0", "ByChange:=" & myChangeCell, "Engine:=1", _
"EngineDesc:=GRG Nonlinear"
Application.Run "SolverOptions", "MaxTime:=0", "Iterations:=0", _ "Precision:=0.00000000001", "Convergence:=0.00000000001", _ "StepThru:=False", "Scaling:=True", "AssumeNonNeg:=True", _ "Derivatives:=1"
Application.Run "SolverOptions", "PopulationSize:=True", "RandomSeed:=0", _
"MutationRate:=0.075", "Multistart:=False", _
"RequireBounds:=True", "MaxSubproblems:=0", _
"MaxIntegerSols:=0", "IntTolerance:=1", "SolveWithout:=False", _
"MaxTimeNoImp:=30"
x = Application.Run("SolverSolve", True)
If x > 3 Then
With Range(myChangeCell)
.HorizontalAlignment = xlRight
.Value = "#Error " & x
End With
End If
End Sub
The only difference is: I replaced direct calls to SolverXXX with calls to Application.Run "SolverXXX" in effort to avoid the need to select the Solver reference, following the paradigm described by Jon Peltier (click
here) [2].
In an effort to isolate the problem, I removed syntax piecewise on the Application.Run "SolverOptions" statement, to no avail.
When I reduce the syntax to just Application.Run "SolverOptions" with no parameters, I no longer get a VBA error. But SolverSolve returns error #13 (coincidentally). Ostensibly, that means: "Error in model. Please verify that all cells and constraints
are valid".
But goodTest works just fine even if I remove all options from the SolverOptions calls. Of course, first I must select the Solver reference.
[1] https://app.box.com/s/x1ts7ceumx46v4gtb95v
[2] http://peltiertech.com/Excel/SolverVBA.html