[英]In Excel to run solver in a loop to optimize a objective cell with constraints
我對 VBA、MACRO 完全陌生。 我需要一個循環來優化一個
准確地說有兩個循環,一個用於列循環 4 次和行循環,直到它結束。
有幾個問題...
Cells(x,y)
,應始終使用工作表對其進行限定。以下代碼應該可以實現您的目標,並標記為適當的修改。
Option Explicit
Sub mySolve()
Dim totalrows As Long
totalrows = 1000 ' *** use whatever formula finds your last row
Dim row As Long
Dim col As Long
Dim myWS As Worksheet
Dim SetAddr As String, ChgAddr As String, ConAddr As String
Set myWS = ActiveSheet
For row = 10 To totalrows Step 9
For col = 14 To 17 Step 1
SetAddr = myWS.Cells(row, col).Address
ChgAddr = myWS.Cells(row, col + 4).Address '*** you don't specify in your question
ConAddr = myWS.Cells(row, col + 4).Address
SolverReset
SolverOk SetCell:=SetAddr, MaxMinVal:=2, ByChange:=ChgAddr, Engine:=3
SolverAdd CellRef:=ConAddr, Relation:=1, FormulaText:="2" '*** what is your constraint
SolverSolve Userfinish:=True
Next col
Next row
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.