Question

I have searched everywhere and attempted to get this to work correctly for hours but my macro will not play ball and the solver hangs at the 'Setting up problem...' stage!

This is what I am trying to do: 1)User starts macro and is prompted to select a range of cells to optimise 2)Solver finds solution and exits

If anyone might be able to suggest how I might get this to work I would be most grateful!

Many thanks,

Rendeverance

This is my code:

Sub Optimise()
Dim UserRange As Range

SolverReset


  Set UserRange = Application.InputBox("Use the mouse to select cells to optimise (Hold       Ctrl to select multiple films)", Type:=8)

    If UserRange Is Nothing Then
        MsgBox "Cancel pressed"
    Else

    'Set solver parameters and solve using GRG Nonlinear
    '
    SolverOk SetCell:="$V$13", MaxMinVal:="$V$15", ValueOf:=0, ByChange:="UserRange", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve UserFinish:=True
    SolverFinish KeepFinal:=1
    End If


End Sub
Was it helpful?

Solution

Thank you to Simoco for identifying the omission of '.Address', but I still could not get the above to work, when defining the MaxMinVal as a cell (instead of just '1' or '2')

So I used a nested IF statement as follows - this works but there may be a more elegant way and if anyone can give me tips on this it would be appreciated:

Sub Optimise()

'
Dim OptiRange As Range

' Activate the sheet required and reset solver settings
Sheets("Dashboard").Activate
SolverReset

' Add-in some bad error handling ;-)
On Error GoTo endofmacro

'Ask user for what cells to optimise
Set OptiRange = Application.InputBox("Use the mouse to select cells to optimise (Hold Ctrl to select multiple films individually)", Type:=8)

        'Check if this is a maximise or minimise problem
        If Range("$Z$15").Value = 1 Then
            '
            'Set solver parameters and solve using GRG Nonlinear
            SolverOk SetCell:="$Z$13", MaxMinVal:=1, ValueOf:=0, ByChange:=OptiRange.Address, Engine:=1, EngineDesc:="GRG Nonlinear"
            SolverSolve UserFinish:=True
            SolverFinish KeepFinal:=1
        Else
            '
            'Set solver parameters and solve using GRG Nonlinear
            SolverOk SetCell:="$Z$13", MaxMinVal:=2, ValueOf:=0, ByChange:=OptiRange.Address, Engine:=1, EngineDesc:="GRG Nonlinear"
            SolverSolve UserFinish:=True
            SolverFinish KeepFinal:=1
        End If

endofmacro:

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top