Next Loop for Solver Macro

Mona9254

New Member
Joined
Mar 12, 2011
Messages
3
Hey I need help making a Next Loop to add to the following macro which uses solver.

Sub Macro7()
'
' Macro7 Macro
'

'
SolverOk SetCell:="$O$41", MaxMinVal:=1, ValueOf:="0", ByChange:="$G$41"
SolverAdd CellRef:="$H$41", Relation:=1, FormulaText:="$T$41"
SolverAdd CellRef:="$H$41", Relation:=3, FormulaText:="$U$41"
SolverAdd CellRef:="$Q$41", Relation:=3, FormulaText:="$V$41"
SolverAdd CellRef:="$K$41", Relation:=3, FormulaText:="$W$41"
SolverAdd CellRef:="$G$41", Relation:=1, FormulaText:="$X$41"
SolverOk SetCell:="$O$41", MaxMinVal:=1, ValueOf:="0", ByChange:="$G$41"
SolverSolve

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Can you explain the layout of your sheet and the range over which the loop is to be iterated?
 
Upvote 0
I want the loop to work from G43 to G234...using the relative cells from the solver example in that row
 
Upvote 0
Im getting a Compile Error: For Without Next.... Can someone help me fix the problem??

Private Sub Worksheet_Activate()
Sub Solverloop()

Dim r As Long
For r = 45 To 234

SolverOk SetCell:="$O$" & r, MaxMinVal:=1, ValueOf:="0", ByChange:="$G$" & r
SolverAdd CellRef:="$H$" & r, Relation:=1, FormulaText:="$T$" & r
SolverAdd CellRef:="$H$" & r, Relation:=3, FormulaText:="$U$" & r
SolverAdd CellRef:="$G$" & r, Relation:=1, FormulaText:="$X$" & r
SolverAdd CellRef:="$K$" & r, Relation:=3, FormulaText:="$W$" & r
SolverAdd CellRef:="$Q$" & r, Relation:=3, FormulaText:="$V$" & r
SolverOk SetCell:="$O$" & r, MaxMinVal:=1, ValueOf:="0", ByChange:="$G$" & r
SolverSolve
End Sub
 
Upvote 0
You are on the right track.

Check Excel VBA for 'for' (w/o the quotes). Then, in the help list click on the 'For...Next statement' link.

Also, you should use a SolverReset to reset the solver parameters at the start of each loop.

Im getting a Compile Error: For Without Next.... Can someone help me fix the problem??

Private Sub Worksheet_Activate()
Sub Solverloop()

Dim r As Long
For r = 45 To 234

SolverOk SetCell:="$O$" & r, MaxMinVal:=1, ValueOf:="0", ByChange:="$G$" & r
SolverAdd CellRef:="$H$" & r, Relation:=1, FormulaText:="$T$" & r
SolverAdd CellRef:="$H$" & r, Relation:=3, FormulaText:="$U$" & r
SolverAdd CellRef:="$G$" & r, Relation:=1, FormulaText:="$X$" & r
SolverAdd CellRef:="$K$" & r, Relation:=3, FormulaText:="$W$" & r
SolverAdd CellRef:="$Q$" & r, Relation:=3, FormulaText:="$V$" & r
SolverOk SetCell:="$O$" & r, MaxMinVal:=1, ValueOf:="0", ByChange:="$G$" & r
SolverSolve
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,700
Members
452,938
Latest member
babeneker

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