Loop Solver through columns

Kerryx

Well-known Member
Joined
May 6, 2016
Messages
740
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Ok first time trying this with solver and putting my hands up my VBA is crap but attempt here to
VBA Code:
Sub Solve()
'
' Solve Macro
'
Dim rng As Range
For i = 4 To 8
Columns(i).Select

SolverReset
    SolverOk SetCell:="$" & i & "$13", MaxMinVal:=3, ValueOf:="39.00", ByChange:="$" & i & "$2:""$" & i & "$7" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$" & i & "$3", Relation:=1, FormulaText:="6"
    SolverAdd CellRef:="$" & i & "$3", Relation:=3, FormulaText:="2"
    SolverAdd CellRef:="$" & i & "$4", Relation:=3, FormulaText:="0.5"
    SolverAdd CellRef:="$" & i & "$4", Relation:=1, FormulaText:="2"
    SolverAdd CellRef:="$" & i & "$6", Relation:=1, FormulaText:="35"
    SolverAdd CellRef:="$" & i & "$6", Relation:=3, FormulaText:="15"
    SolverAdd CellRef:="$" & i & "$7", Relation:=3, FormulaText:="2"
    SolverAdd CellRef:="$" & i & "$7", Relation:=3, FormulaText:="2"
   
    SolverSolve UserFinish:=True
    SolverFinish KeepFinal:=1
    Next i
    
End Sub
Trying to get total to 39 by adjusting values in c3 to c7 with variables as seen in code below and so on across the columns, seems to jump across columns ok but see no change in data, what am i doing wrong?
Thanks for the helps folks.
Project hrs.xlsm
BCDEF
1JobsWeek1Week2Week3Week4
2a2.511.253
3b5.5547
4c1.5221
5d3.833333
6e19.83333172921
7f5.833333237
8g
9h
10i
11j
12k
13Total39263836
Sheet1
Cell Formulas
RangeFormula
C13:F13C13=SUM(C2:C12)
Named Ranges
NameRefers ToCells
solver_lhs1=Sheet1!$C$3C13
solver_lhs10=Sheet1!$C$3C13
solver_lhs11=Sheet1!$C$4C13
solver_lhs12=Sheet1!$C$4C13
solver_lhs13=Sheet1!$C$6C13
solver_lhs14=Sheet1!$C$6C13
solver_lhs15=Sheet1!$C$7C13
solver_lhs16=Sheet1!$C$7C13
solver_lhs17=Sheet1!$C$3C13
solver_lhs18=Sheet1!$C$3C13
solver_lhs19=Sheet1!$C$4C13
solver_lhs2=Sheet1!$C$3C13
solver_lhs20=Sheet1!$C$4C13
solver_lhs21=Sheet1!$C$6C13
solver_lhs22=Sheet1!$C$6C13
solver_lhs23=Sheet1!$C$7C13
solver_lhs24=Sheet1!$C$7C13
solver_lhs25=Sheet1!$C$3C13
solver_lhs26=Sheet1!$C$3C13
solver_lhs27=Sheet1!$C$4C13
solver_lhs28=Sheet1!$C$4C13
solver_lhs29=Sheet1!$C$6C13
solver_lhs3=Sheet1!$C$4C13
solver_lhs30=Sheet1!$C$6C13
solver_lhs31=Sheet1!$C$7C13
solver_lhs32=Sheet1!$C$7C13
solver_lhs33=Sheet1!$C$3C13
solver_lhs34=Sheet1!$C$3C13
solver_lhs35=Sheet1!$C$4C13
solver_lhs36=Sheet1!$C$4C13
solver_lhs37=Sheet1!$C$6C13
solver_lhs38=Sheet1!$C$6C13
solver_lhs39=Sheet1!$C$7C13
solver_lhs4=Sheet1!$C$4C13
solver_lhs40=Sheet1!$C$7C13
solver_lhs5=Sheet1!$C$6C13
solver_lhs6=Sheet1!$C$6C13
solver_lhs7=Sheet1!$C$7C13
solver_lhs8=Sheet1!$C$7C13
solver_lhs9=Sheet1!$C$3C13
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You are trying to change columns, but your variable, i, is an integer type (as you haven't declared it and you assign it an integer vallue)
So your code uses something like $4$13 for a reference (address), while you need something like $D$13 and so on

To change number into character one coul use chr function.
chr(65) = A, chr(66) = B, and so on.

(other option could be cells(i,13).address to get (provided i = 4) $D$13)


So try:
VBA Code:
For i = 3 To 7  ' you want to start from column C, right? it is 3rd column
' this is unneded Columns(i).Select
colname = chr(64+i)
SolverReset
    SolverOk SetCell:="$" & colname & "$13", MaxMinVal:=3, ValueOf:="39.00", ByChange:="$" & colname & "$2:""$" & colname & "$7" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"
' etc.

Make sure you have a reference to Solver in VBA Editor (Alt+F11 Alt+T R and make sure checkbox next to Solver is checked)
 
Upvote 0
Duh should've spotted that big difference between calling numbers and letters
OK updated the code to the below , not throwing up any errors but does not seem to do anything, also have the reference ticked
1707045440754.png

VBA Code:
Sub Solve()
'
' Solve Macro
'
For i = 3 To 7  ' you want to start from column C, right? it is 3rd column
' this is unneded Columns(i).Select

colname = Chr(64 + i)

SolverReset
    SolverOk SetCell:="$" & colname & "$13", MaxMinVal:=3, ValueOf:="39.00", ByChange:="$" & colname & "$3:""$" & colname & "$7" _
        , Engine:=1, EngineDesc:="GRG Nonlinear"

    SolverAdd CellRef:="$" & colname & "$3", Relation:=1, FormulaText:="6"
    SolverAdd CellRef:="$" & colname & "$3", Relation:=3, FormulaText:="2"
    SolverAdd CellRef:="$" & colname & "$4", Relation:=3, FormulaText:="0.5"
    SolverAdd CellRef:="$" & colname & "$4", Relation:=1, FormulaText:="2"
    SolverAdd CellRef:="$" & colname & "$6", Relation:=1, FormulaText:="35"
    SolverAdd CellRef:="$" & colname & "$6", Relation:=3, FormulaText:="15"
    SolverAdd CellRef:="$" & colname & "$7", Relation:=3, FormulaText:="2"
    SolverAdd CellRef:="$" & colname & "$7", Relation:=3, FormulaText:="2"
   
    SolverSolve UserFinish:=True
    SolverFinish KeepFinal:=1
    Next i
       
End Sub
 
Upvote 0
Two corections in SolverOk:

number 39 in ValueOf
Not text.
if needed with decimal then 39.15 and so on, but not text "39.00"
VBA Code:
ValueOf:=39

and correct range (no extra double quotations):
VBA Code:
ByChange:="$" & colname & "$3:$" & colname & "$7"

But as a matter of fact you can write all addresses as relative, so:
VBA Code:
    SolverOk SetCell:=colname & "13", MaxMinVal:=3, ValueOf:=39, ByChange:=colname & "3:" & colname & "7", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:=colname & "3", Relation:=1, FormulaText:="6"
and so on. Note that FormulaText remains text - in doublequotation marks.

You may also add just before End Sub two lines to ensure that after Solver finishes the excel takes "full control". I noticed that sometimes after executing solver in a loop severel times, worksheet does not show final result until you take some actions (like recalculate). But as I said - you may add it, as well as you may use relative addressing, but above mentioned changes - correcting the range and using number, not text in ValueOf are compulsory.
VBA Code:
Application.Calculate
Application.ScreenUpdating = True
 
Upvote 0
Solution
Kaper, thank you very much , added the last 2 lines as you suggested to just before the loop continues i.e. before "Next i" , if i left them after it, just shows result for last column rather than all columns individually, so bang on for that, a great help. (y)
 
Upvote 0
Glad to hear that it worked. And thanks for the important comment on recalculation and screen updating location.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

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