简体   繁体   中英

I need to automate Excel solver by using Macro . I have a problem in making "set objective value" a reference cell not a hard code

I am using the solver to build efficient portfolio of stocks. I must automate the solver in full.

My problem is how to input in solver window slot " set objective value of " to be a reference cell and NOT a hard code value . Of course the reference cell shall have different values whenever we change the input / stocks selection.

Therefore , It is a crucial step to make this "objective value of " to be a reference cell in order to fully automate my solver module.

I am average user . Pls advise me what are the VBA sentences I should add and change in the Macro VBA to make my Macro run automatically & smoothly .

Sub Macro6()
'
' Macro6 Macro
' Automate Solver
'
' Keyboard Shortcut: Ctrl+Shift+K
'
    ActiveWindow.SmallScroll Down:=-66
    Range("A1").Select
    SolverReset
    SolverOk SetCell:="$D$39", MaxMinVal:=2, ValueOf:=0, ByChange:="$Y$14:$Y$33", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$Y$34", Relation:=2, FormulaText:="1"
    SolverAdd CellRef:="$Y$14:$Y$33", Relation:=3, FormulaText:="0"
    SolverOk SetCell:="$D$39", MaxMinVal:=2, ValueOf:=0, ByChange:="$Y$14:$Y$33", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$D$39", MaxMinVal:=2, ValueOf:=0, ByChange:="$Y$14:$Y$33", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve
    SolverOk SetCell:="$D$39", MaxMinVal:=2, ValueOf:=0, ByChange:="$Y$14:$Y$33", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$D$38", MaxMinVal:=3, **ValueOf:=0.154**, ByChange:= _
        "$Y$14:$Y$33", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$D$38", MaxMinVal:=3, **ValueOf:=0.154**, ByChange:= _
        "$Y$14:$Y$33", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve
    SolverOk SetCell:="$D$38", MaxMinVal:=3, ValueOf:=0.154, ByChange:= _
        "$Y$14:$Y$33", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$D$38", MaxMinVal:=3, **ValueOf:=0.172**, ByChange:= _
        "$Y$14:$Y$33", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$D$38", MaxMinVal:=3, **ValueOf:=0.172**, ByChange:= _
        "$Y$14:$Y$33", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve
    SolverOk SetCell:="$D$38", MaxMinVal:=3, ValueOf:=0.172, ByChange:= _
        "$Y$14:$Y$33", Engine:=1, EngineDesc:="GRG Nonlinear"
End Sub

The above bold values 0.154 & 0.172 needs to be replaced with cell reference and NOT a hard code. for example i need to replace 0.154 to be " ValueOf:=target1 and 0.172 to be " ValueOf:=target2 " ....etc . pls advise.

Declare a variable, give it a value, and then use it where you're currently using a hard-coded value. That is:

Dim TargetValue As Double

...

TargetValue = 0.514
SolverOk SetCell:="$D$39", MaxMinVal:=3, ValueOf:=TargetValue, ByChange:="$Y$14:$Y$33", _
    Engine:=1, EngineDesc:="GRG Nonlinear"

Or you can get the value from a cell, eg A1:

TargetValue = Range("A1").Value

Also, you can remove the many duplicate lines of code, to make things simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM