Excel 2016 VBA solver constraints
Excel 2016 VBA solver constraints
I am trying to run an automatic solver over a column of values. Target, reference and constraints cells are all in the same row, and they shift down with the target cell while running the solver throughout the column. The target cell has to be minimized, by changing the reference cell on the same row, under the constrain requiring to keep a third cell (that depends on the reference cell value) greater or equal than a fourth cell. None of the cells contain integers, all of them contain decimal values.
To resume:
Target cell (to be minimized) : P13
Reference cell (changing cell) : L13
Constrain (where M13 depends on L13) : M13 >= F13
all down to row 8795, shifting accordingly all the cells before mentioned.
The solver works fine running it manually row by row, but trying to automatize it in order to avoid 8762 iterations it doesn't respect the constrain. I have tried to remove " ", to add SolverReset and to put the constraint on top of the instructions, but nothing worked well. The code is:
Private Sub CommandButton1_Click()
Dim count As Integer
count = 13
Do While count <= 8795
SolverOk SetCell:=Cells(count, 16), MaxMinVal:=2, ByChange:=Cells(count, 12)
SolverAdd CellRef:=Cells(count, 13), Relation:=3, FormulaText:=Cells(count, 6)
SolverSolve userfinish:=True
count = count + 1
Loop
End Sub
I was trying to change constraint and I have noticed where probably the bug is: after running the code, if I open the solver I can see the last constraint used by the code. As you can see in the image below, I've tried to set a new constraint L13<=H13, where H13= 9,272 but the solver has 9272190.... maybe it is not "seeing" the decimal separator. Any idea?
Thank you for your time.
SolverReset
SolverRestart
Yes, you are right, typing error. However, the outcome is the same and it doesn't consider the constraint while iterating.
– Luca91
Aug 27 at 12:36
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is it not
SolverReset
instead ofSolverRestart
?– Pierre44
Aug 27 at 12:27