我对VBA,MACRO完全不熟悉。我需要一个循环来优化
有两个循环是精确的,一个用于列循环4次和行循环,直到它到达结束。
答案 0 :(得分:0)
有几个问题......
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