【问题标题】:VBA Loop in Excel with Solver带有求解器的 Excel 中的 VBA 循环
【发布时间】:2016-03-14 19:56:37
【问题描述】:

所以这是我的问题。我正在尝试编写一个使用求解器执行功能的循环。基本上它从一个单元格开始,运行求解器,将目标单元格和约束单元格移动到右侧,求解,将目标单元格和约束单元格向右移动 14,求解,右侧,求解,右侧 14 求解,直到找到空白。

这是前几行代码,向您展示我在长版本中所做的事情。

Range("RI6").Select
    SolverOk SetCell:="$RI$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RG$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$RI$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$RI$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("RJ16").Select
    SolverOk SetCell:="$RJ$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RJ$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$RJ$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$RJ$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("RX16").Select
    SolverOk SetCell:="$RX$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RX$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$RX$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$RX$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("RY16").Select
    SolverOk SetCell:="$RY$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RY$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$RY$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$RY$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("SM16").Select
    SolverOk SetCell:="$SM$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$SM$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$SM$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$SM$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("SN16").Select
    SolverOk SetCell:="$SN$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$SN$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$SN$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$SN$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("TB16").Select
    SolverOk SetCell:="$TB$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TB$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$TB$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$TB$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("TC16").Select
    SolverOk SetCell:="$TC$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TC$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$TC$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$TC$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("TQ16").Select
    SolverOk SetCell:="$TQ$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TQ$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$TQ$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$TQ$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("TR16").Select
    SolverOk SetCell:="$TR$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$TR$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$TR$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$TR$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("UF16").Select
    SolverOk SetCell:="$UF$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$UF$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$UF$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$UF$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)
Range("UG16").Select
    SolverOk SetCell:="$UG$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$UG$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$UG$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$UG$1", Relation:=1, FormulaText:="87"
    SolverSolve (True) 

提前感谢您的任何帮助/建议。

【问题讨论】:

  • 您似乎没有任何问题。你的代码真的有效吗?如果是这样 - 您可以使用循环来缩短它。
  • 如果您在编写循环时需要帮助,那么您可能需要查看以下网站:homeandlearn.org/excel_vba_for_loops.html
  • 我的问题是如何进行单元格移动,我试图循环它并且无法使其正常工作。该代码在写出很长的路时有效,但在我尝试循环时无效。

标签: vba excel


【解决方案1】:

未经测试,但可能类似于...

Option Explicit

Sub SolverLop()
Dim i As Long
Dim cnt As Integer
cnt = 0
For i = 477 To ActiveSheet.UsedRange.Columns.Count

    SolverReset
    SolverOk SetCell:=Cells(16, i).Address, MaxMinVal:=3, ValueOf:=0.15, ByChange:=Cells(16, i).Address, _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:=Cells(1, i).Address, Relation:=3, FormulaText:="74"
    SolverAdd CellRef:=Cells(1, i).Address, Relation:=1, FormulaText:="87"
    SolverSolve Userfinish:=True

    cnt = cnt + 1
    If cnt = 2 Then
        i = i + 14
        cnt = 0
    End If
Next

End Sub

确保您在工具--> 参考--> 求解器中选中了对求解器的引用。

附:在上面的示例中,477 是与起始列“RJ”关联的列号...要获取任何列的列号,只需在空白单元格=column(indirect("A2")) 中键入以下内容...将“A2”替换为您要为其查找编号的列。

【讨论】:

  • 我正在做一些非常相似的事情,但你的更干净。您应该分享 477 是列号,或者输入一个转换,以便 OP 可以只输入起始范围并从中收集列 #。
【解决方案2】:

第一部分看起来与其他部分不同,所以我没有将它包含在“循环”中,但如果差异是由于拼写错误造成的,添加起来很容易。

    Range("RI6").Select
    SolverOk SetCell:="$RI$16", MaxMinVal:=3, ValueOf:=0.15, ByChange:="$RG$1", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$RI$1", Relation:=3, FormulaText:="74"
    SolverAdd CellRef:="$RI$1", Relation:=1, FormulaText:="87"
    SolverSolve (True)

所以一种循环方式如下:

    Dim Temp As String, TempArray As Variant

    TempArray = Array("RJ16", "RX16", "RY16", "SM16", "SN16", "TB16", "TC16", "TQ16", "TR16", "UF16", "UG16")
    For i = 0 To UBound(TempArray)
        Range(TempArray(i)).select
        Temp = Cells(1, Range(TempArray(i)).Column).Address
        SolverOk SetCell:=TempArray(i), MaxMinVal:=3, ValueOf:=0.15, ByChange:=Temp, Engine:=1, EngineDesc:="GRG Nonlinear"
        SolverAdd CellRef:=Temp, Relation:=3, FormulaText:="74"
        SolverAdd CellRef:=Temp, Relation:=1, FormulaText:="87"
        SolverSolve (True)
    Next i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2018-02-17
    • 2013-02-07
    相关资源
    最近更新 更多