下拉列表的宏

时间:2014-07-15 13:29:09

标签: excel vba

Sub SolverMacro()
'
' SolverMacro Macro
'

'
    Range("F29").Select
    SolverOk SetCell:="$F$29", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$18:$D$26", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$F$29", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$18:$D$26", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve
    ActiveWindow.SmallScroll Down:=-12

    If (Range("f29").Value > Range("b2").Value) Then
        MsgBox "Not Feasible"
    Else
        MsgBox "Feasible"
    End If


End Sub

您好,我点击一个按钮,按钮运行上面的宏。它执行求解器功能。 但是我希望通过下拉列表运行100个条件。如何将上述内容置于for循环中并依次运行求解器100次。下拉列表位于单元格A2中。谢谢。

1 个答案:

答案 0 :(得分:0)

这就是你想要的吗?

For i = 1 to 100

    'Change "MyDropDownList" to your DDL's name
    MyDropDownList.ClearSelection()
    MyDropDownList.Items(i).Selected = True

    Range("F29").Select
    SolverOk SetCell:="$F$29", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$18:$D$26", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverOk SetCell:="$F$29", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$18:$D$26", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve

    'Repeating the scroll in a loop is not a good idea IMO, you should just remove it
    'ActiveWindow.SmallScroll Down:=-12

    If (Range("f29").Value > Range("b2").Value) Then
        MsgBox "Not Feasible"
    Else
        MsgBox "Feasible"
    End If
Next