I am running a vba code as follows:
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
As you can see the first half of the code is repeated again. The range changes from N3 to N48 , N4 to N49 and so on.
Now I have to keep repeating the entire code for 172 iterations only changing the range Part which is the cell references to the solver location.
Is there some way I can repeat the process for 172 times using for loop by some how changing the references after each iteration.
All the references increase by +45 each iteration, as is visible.
Try this. It's quite possible I have some of the numbers off but hopefully you get the idea
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.