简体   繁体   中英

VBA Solver Constraints Not Set Error (w/ no constraints specified)

Firstly, I have tried researching the problem I have but the solutions I have found (for example, here , have not worked for me). I am trying to write a macro that uses the (normally) straightforward Solver functionality of solving for a value.

The code I have got to is as follows:

Sub SolverAEP()

    Dim GeneratedPower As Double
    GeneratedPower = Worksheets("AEP").Range("D57")

    SolverReset

    SolverOk SetCell:="$D$58", _
         MaxMinVal:=3, _
         ValueOf:=GeneratedPower, _
         ByChange:="$D$65" _
         , Engine:=1, EngineDesc:="GRG Nonlinear"

    SolverSolve userFinish:=False    

End Sub

The error at present is that Solver could not find a feasible solution. Sovler can not find a point for which all Constraints are satisified. Solver could not find a feasible solution. Sovler can not find a point for which all Constraints are satisified.

What I have tried:

  • Some online guidance says that the ValueOf needs to be a string - I tried the CStr approach to both Range("D57") and Cells(57,"D").Value and both came up with a Solver error.
  • When I manually try to open Solver, it always opens up with a comma instead of a full stop, so I used a small script to replace all commas with a full stop, but that did not change anything (only tried this when I was dealing with a string as the ValueOf . [I am not sure why this actually happens, but I think there's some sort of a culture conflict as I'm using both a Continental and UK keyboard and localisation settings are fairly random.]
  • Recording the macro doing the same manually in Solver gives me the below as the Solver code:

     Sub Macro2() SolverOk SetCell:="$D$58", MaxMinVal:=3, ValueOf:=21000#, ByChange:="$D$65" _ , Engine:=1, EngineDesc:="GRG Nonlinear" SolverOk SetCell:="$D$58", MaxMinVal:=3, ValueOf:=21000#, ByChange:="$D$65" _ , Engine:=1, EngineDesc:="GRG Nonlinear" SolverSolve End Sub 
  • The values I presently have could be rounded if necessary - as it seems when I stepped through my written macro that the decimal was still using a comma instead of a full stop. I tried rounding the value and putting that in there, but the same error came up.

Lastly, I should add that there is no problem in solving this manually with the Solver. The referenced cell D58 is a sum of other cells, the value of which depends on the value of D65.

Can anyone highlight what I am doing wrong please?

In case it matters, I'm using Excel 2016.

It appears as if you are not activating the sheet on which your formulae exist, and therefore the Solve is doing its process on whatever the currently Active sheet is.

I recommend changing your code to:

Sub SolverAEP()

    With Worksheets("AEP")
        .Activate 

        SolverReset

        SolverOk SetCell:="$D$58", _
                 MaxMinVal:=3, _
                 ValueOf:=[$D$57], _
                 ByChange:="$D$65", _
                 Engine:=1, _
                 EngineDesc:="GRG Nonlinear"

        SolverSolve userFinish:=False
    End With

End Sub

The Activate will ensure that the correct sheet is selected when running the Solver. (Much as I hate the use of Activate , Select , Selection , etc, this is one of those times it is required - the Solver will only work on the Active sheet.)


Re your comment about values not being updated until after you run the Solver a second time if you have an Application.ScreenUpdating = False statement included, I can't think of any reason other than the fairly obvious question "have you included a Application.ScreenUpdating = True statement as well?".

I tried running it with just the =False and running SolverAEP caused the screen to be updated once the macro ended.

However, if I wrote another subroutine such as the following:

Sub Test
    SolverAEP
    MsgBox "Finished first solve"
    SolverAEP
    MsgBox "Finished second solve"
End Sub

and ran it, then (with just a =False and no corresponding =True ) the screen does not update until the End Sub of Sub Test is reached. This is the expected behaviour of having stopped ScreenUpdating .

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