2016-03-14 61 views
0

所以這是我的問題。我正在嘗試編寫一個使用求解器執行函數的循環。基本上它從一個單元開始,運行求解器,將目標和約束單元移動到正確的單元,解決,移動目標和約束單元到右邊14,解決,右邊一個,解決,右邊14解決,直到它找到一個空白。Excel中的VBA循環與求解器

這裏是前幾行代碼,告訴你我在長版本中做什麼。

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

您似乎沒有任何問題。你的代碼是否真的有用?如果是這樣 - 你可以通過循環縮短它。 –

+0

如果你需要編寫循環的幫助,那麼你可能想看看下面的網站:http://www.homeandlearn.org/excel_vba_for_loops.html – Ralph

+0

我的問題是如何做細胞運動,我試圖循環,不能讓它正常工作。代碼在寫出很長的路時起作用,但不是當我嘗試循環時。 –

回答

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 
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」替換爲列想要找到的號碼。

+0

我正在研究一些非常相似的東西,但是你的更清潔。您應該分享477是列號,或者輸入一個轉換,以便OP可以輸入起始範圍,並從中收集列號#。 –