带解算器的循环的VBA结果记录

时间:2014-09-12 14:11:30

标签: excel vba loops excel-vba solver

我想使用求解器来优化非线性问题的系统。对于每个循环,我想记录改变的细胞结果,即细胞(5,4),细胞(5,5),细胞(5,6),细胞(5,7)。但我的代码似乎只记录第一个循环结果。

Sub Macro1()
'
' Macro1 Macro
'

'
Dim lIncrement As Long
lIncrement = 10
For Counter1 = 10 To 12
For Counter2 = 10 To 20 Step lIncrement
Cells(18, 2) = Counter1
    Cells(11, 2) = Counter2
    SolverOk SetCell:="$H$16", MaxMinVal:=1, ValueOf:=0, ByChange:="$D$5:$G$5", _
        Engine:=3, EngineDesc:="Evolutionary"
    SolverOk SetCell:="$H$16", MaxMinVal:=1, ValueOf:=0, ByChange:="$D$5:$G$5", _
        Engine:=3, EngineDesc:="Evolutionary"
    SolverSolve userFinish:=True
    SolverFinish KeepFinal:=1
    Cells(21 + Counter1, 1 + Counter2 / lIncrement).Value = Cells(5, 4) & "," & Cells(5, 5) & "," & Cells(5, 6) & "," & Cells(5, 7)
    Next Counter2
Next Counter1

End Sub

0 个答案:

没有答案