Dynamic Solver with do loop

Marc__

New Member
Joined
May 25, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello, everyone,

I am trying to dynamize a loop so that I can apply it to other tables. Here is a short explanation of how the loop should work:

The loop should search in column I from row O + i to see if there is a value in the cells. If there is a value in a cell, the value in column AW in this row should be minimized by the solver. The solver may change the weights (columns AP, AR and AT). Constraints are that the values in the columns AQ, AS and AU should be equal. The sum of the three weights must be 1 (column AV). The loop should be executed until there are no more values in the cells in column A.
If I rewrite the code for only one cell (without O + i but only one line number), it works. So the constraints should not be a problem.
The code should be used for several folders, so I would like to have it dynamic.

VBA Code:
Sub DoLoop_dynamic_Solver

Dim i As Integer
Dim O As Integer
i = i + 1
O = Cells(6, 13)

Do
If Cells(O + i, 9) = "" Then
i = i + 1
ElseIf Cells(O + i, 9) <> 0 Then

SolverReset
    SolverOk SetCell:="$AW$ O + i", MaxMinVal:=2, ValueOf:=0, ByChange:= _
        "$AP$ O + i,$AR$ O + i,$AT$ O + i", Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$AQ$ O + i", Relation:=2, FormulaText:="$AS$ O + i"
    SolverAdd CellRef:="$AQ$ O + i", Relation:=2, FormulaText:="$AU$ O + i"
    SolverAdd CellRef:="$AS$ O + i", Relation:=2, FormulaText:="$AU$ O + i"
    SolverAdd CellRef:="$AV$ O + i", Relation:=2, FormulaText:="1"
    SolverSolve 
    i = i + 1
End If
    Loop While Cells(O + i, 1) <> ""
End Sub

Do any of you have an idea what I did wrong or could change?

Thanks a lot for your help! :)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top