我正在运行vba代码,如下所示:
Dim score As Integer
SolverReset
score = Range("N3").Value
If score = 1 Then
SolverLoad loadArea:=Range("N4:N11")
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
SolverReset
SolverLoad loadArea:=Range("O4:O14")
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
SolverReset
Else:
SolverReset
SolverLoad loadArea:=Range("P4:P15")
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
End If
SolverReset
score = Range("N48").Value
If score = 1 Then
SolverLoad loadArea:=Range("N49:N56")
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
SolverReset
SolverLoad loadArea:=Range("O49:O59")
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
SolverReset
Else:
SolverReset
SolverLoad loadArea:=Range("P49:P60")
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
End If
正如您所看到的,代码的前半部分再次重复。范围从 N3变为N48 , N4变为N49 等等。
现在我必须继续重复172次迭代的整个代码,只更改范围Part,它是求解器位置的单元格引用。
我是否有某种方法可以使用for循环重复该过程172次,以及在每次迭代后如何更改引用。
所有引用每次迭代增加+45,如可见。
答案 0 :(得分:1)
试试这个。我很可能会把一些数字关掉,但希望你能得到这个想法
Sub x()
Dim score As Long, r As Range, i As Long
Set r = Range("N3")
For i = 1 To 172
SolverReset
score = r.Value
If score = 1 Then
SolverLoad loadArea:=r.Offset(1).Resize(8)
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
SolverReset
SolverLoad loadArea:=r.Offset(1, 1).Resize(11)
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
SolverReset
Else:
SolverReset
SolverLoad loadArea:=r.Offset(1, 2).Resize(12)
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
End If
Set r = r.Offset(45)
Next i
End Sub