Store Solver Result with For and Next loop in Exce

2019-09-10 18:25发布

问题:

I want to use repeat solver for 51 times and store the result from each computation of solver in a separate cell in Excel. Here is my VBA script below; however, I run into a problem with the script. The stored result only shows the 51st, the last computation result. It is a simple script but I could figure out where I did wrong. Thank you for the help!

Private Sub Button1_Click()
Dim j As Integer
For j = 1 To 51
Range("m4") = j
solversolve UserFinish:=True
solversolve(UserFinish:=True) = Range("Q" & j + 3)
Next j
End Sub

回答1:

Assuming the objective function is in Range ("m4") and you want to solve the solutions beginning at Range ("Q4") then try:

Private Sub Button1_Click()`  
  Dim j As Integer
  For j = 1 To 51
    solversolve UserFinish:=True
    Range("Q" & j + 3).value=Range("m4").value
  Next j
End Sub

But, you don't make changes to the model to re-run it? Have any random values?