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.
Do any of you have an idea what I did wrong or could change?
Thanks a lot for your help!
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!