mikeJW
Mechanical
- May 2, 2003
- 21
I am running an Excel VBA programme where the VBA code calls the Solver and moves on immediately without waiting on the Solver. In fact the Solver does not run! :-(
When running in the development environment it works!
When starting the code via an event such as command button press, it fails!
I now understand the problem and have a solution so I have published the information for anyone else who encounters this.
Sequence:
VBA code starts running in Sheet1.
Sheet1 calls a procedure in Sheet2
Sheet2 procedure ends by calling the Solver.
Code returns to Sheet1 and continues without the Solver starting.
It is not an error in initializing the Solver but it is an anomaly in the VBA.
Moving the Solver call and the cells associated with the Solver to Sheet1 runs OK!
So it seems that code started in one sheet will not recognise the Solver if it is called from another sheet.
Solution1
Make sure when starting any code which eventually calls the Solver, that the Solver call and associated cells are on the starting sheet.
Solution2
Write your own ‘Solver’ it is a simple iteration of successive estimates between constraints.
I could have/should have gone this way but I wanted to know why the Solver was not running.
For your interest my Solver initialisation: 'Needs Project reference 'Solver'
Public Sub UseSolverQ()
SolverReset
SolverOptions Precision:=0.001
SolverAdd CellRef:="DesVar", Relation:=3, FormulaText:="Con1"
SolverAdd CellRef:="DesVar", Relation:=1, FormulaText:="Con2"
SolverOk SetCell:="TargetF", MaxMinVal:=3, ValueOf:="0", ByChange:="DesVar"
SolverSolve True 'True omits dialog
End Sub
DesVar is the design variable = in this case a range name
Relation relationship between the left/right sides of the equations
1 <= 2 = 3 >= 4 cell reference is an integer
FormulaText is the range name to go with the relationship
SolverAdd CellRef:="DesVar", Relation:=3, FormulaText:="Con1"
means the value in ‘DesVar’ >= the value in Con1
TargetF is the equation I am solving
MaxMinVal 1 = solve for minimum, 2 = solve for maximum, 3 = solve for specific value
Value of 0 = I am trying to solve for the equation = 0
ByChange by changing the value in ‘DesVar’
SolverOk SetCell:="TargetF", MaxMinVal:=3, ValueOf:="0", ByChange:="DesVar" means
the equation in 'DesVar' is specifically equal to the value 0
When running in the development environment it works!
When starting the code via an event such as command button press, it fails!
I now understand the problem and have a solution so I have published the information for anyone else who encounters this.
Sequence:
VBA code starts running in Sheet1.
Sheet1 calls a procedure in Sheet2
Sheet2 procedure ends by calling the Solver.
Code returns to Sheet1 and continues without the Solver starting.
It is not an error in initializing the Solver but it is an anomaly in the VBA.
Moving the Solver call and the cells associated with the Solver to Sheet1 runs OK!
So it seems that code started in one sheet will not recognise the Solver if it is called from another sheet.
Solution1
Make sure when starting any code which eventually calls the Solver, that the Solver call and associated cells are on the starting sheet.
Solution2
Write your own ‘Solver’ it is a simple iteration of successive estimates between constraints.
I could have/should have gone this way but I wanted to know why the Solver was not running.
For your interest my Solver initialisation: 'Needs Project reference 'Solver'
Public Sub UseSolverQ()
SolverReset
SolverOptions Precision:=0.001
SolverAdd CellRef:="DesVar", Relation:=3, FormulaText:="Con1"
SolverAdd CellRef:="DesVar", Relation:=1, FormulaText:="Con2"
SolverOk SetCell:="TargetF", MaxMinVal:=3, ValueOf:="0", ByChange:="DesVar"
SolverSolve True 'True omits dialog
End Sub
DesVar is the design variable = in this case a range name
Relation relationship between the left/right sides of the equations
1 <= 2 = 3 >= 4 cell reference is an integer
FormulaText is the range name to go with the relationship
SolverAdd CellRef:="DesVar", Relation:=3, FormulaText:="Con1"
means the value in ‘DesVar’ >= the value in Con1
TargetF is the equation I am solving
MaxMinVal 1 = solve for minimum, 2 = solve for maximum, 3 = solve for specific value
Value of 0 = I am trying to solve for the equation = 0
ByChange by changing the value in ‘DesVar’
SolverOk SetCell:="TargetF", MaxMinVal:=3, ValueOf:="0", ByChange:="DesVar" means
the equation in 'DesVar' is specifically equal to the value 0