所以这是我的问题。我正在尝试编写一个使用求解器执行函数的循环。基本上它从一个单元格开始,运行求解器,将目标和约束单元移动到右边,解决,将目标和约束单元移动到右边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)
提前感谢您提供任何帮助/建议。
答案 0 :(得分: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
确保您在“工具”中检查了解算器 - >参考 - >解算器。
P.S。 477是与上面示例中的起始列“RJ”关联的列号...要获取任何列的列号,只需在空白单元格中键入以下内容=column(indirect("A2"))
...替换“A2”在列中您想要找到的数字。
答案 1 :(得分:0)
第一部分看起来与其他部分不同,所以我没有把它包含在"循环"中,但是如果差异是由于拼写错误就很容易添加。
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