0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

you could also do step 45 in your for loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.