VBA中Excel Solver函数的用户定义范围

敬业度

我到处搜索,试图使它正常工作几个小时,但我的宏无法打球,而且求解器在“设置问题...”阶段挂起!

这是我要尝试的操作:1)用户启动宏,并提示您选择要优化的单元格范围2)求解器找到解决方案并退出

如果有人可以建议我如何使它工作,我将不胜感激!

非常感谢,

敬业度

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
敬业度

感谢Simoco确定了'.Address'的遗漏,但是当将MaxMinVal定义为一个单元格(而不只是'1'或'2')时,我仍然无法使上面的方法起作用

因此,我使用了一个嵌套的IF语句,如下所示-可以工作,但是可能会有一种更优雅的方法,如果有人可以给我提示,将不胜感激:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章