• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA, Macros for Solver

Yash0071

New Member
Hello,

>>> as You've already noted <<<
>>> use code - tags <<<
I'm working on Variance optimization from 2 days with VBA support

Code:
 (Solver)
Sub TestSolver()
    SolverOk SetCell:="$H$13", MaxMinVal:=1, ByChange:="$G$2:$G$6", Engine:=1, EngineDesc:="GRG Nonlinear"
    ' Add criteria - F3 must be equal to 1
    SolverAdd CellRef:="$G$7", Relation:=2, FormulaText:="1"
    ' Add criteria - H13 must be equal to 18
    SolverAdd CellRef:="$H$13", Relation:=2, FormulaText:="18"

    ' Find a solution by solving the problem
    SolverSolve
End Sub

I have big issues due to this formula. All the cell formulas are vanishing off after running this macro. Plus the final result 18 is not coming but the max result in optimization is outputted. Can anyone help me to protect my cell formulas against solver? Also help on getting 18 result.

Forgot to mention, recorded macros and it made my excel sheet more worse but the macro is working. Im using 365 I believe which is standard right now.

I got a message saying use Code tags, does it mean I have to type CODE: at the beginning please?

Regards
 

Attachments

  • t (version 1).xlsm
    33.4 KB · Views: 2
Last edited:

Yash0071

As You've read all links from
You've found page
You've found one way - how to use code - tags.
 
Im working on my question today and cross referenced query with a different code on MS forums. Some of the problems about formula corruption could be stopped by using this code. But the output is not working because its returning False condition for a constraint.


Code:
Sub MaxSolver()
Dim ws As Worksheet
Dim formulaCells As Range   
Set ws = Worksheets("MANUAL")    ws. Activate   
' Lock formula cells   
Set formulaCells = Range("G2:G6")   
formulaCells.Locked = True   
ws. Protect UserInterfaceOnly:=True   
SolverReset    SolverOptions precision:=0.001   
SolverOK setCell:=Range("H16"), _             
maxMinVal:=1, _             
byChange:=formulaCells             
SolverAdd cellRef:=Range("G7"), _             
 relation:=2, _             
 formulaText:=1   
SolverAdd cellRef:=Range("H13"), _             
relation:=1, _             
formulaText:=22 
' Set target value for H13   
SolverSolve userFinish:=False   
SolverSave saveArea:=Range("H20")   
' Unlock formula cells   
formulaCells.Locked = False   
ws. Protect UserInterfaceOnly:=True, _               
AllowFormattingCells:=True
End Sub
 
Back
Top