Looping VBA with Solver Question

Rollnation

New Member
Joined
Jan 17, 2017
Messages
17
Novice question...
I am trying to loop Solver through a specified array of worksheets in a workbook twice. With a Copy&paste values routine in between.
Currently the routines will work if run separately but to combine them I believe I need to make the entire sub option explicit because I start to get compile errors.


Objective
1. Run will solver to to minimize values in X cells through each worksheet in array
2. Copy and paste values in through each worksheet in array
3. Run will solver to to minimize values in Y cells through each worksheet in array

I am trying to use similar code to accomplish these three tasks but am not sure how to get them to run together. Do I need to make each option explicit and change the variables?

My code:

The sheetlist array is much larger but I have condensed it here

1.
Code:
Sub SolverLoop()


Sheetlist = Array("Total_SanDiego", "Total_Ventura") 


Worksheets(Sheetlist(i)).Activate


'Initiate smoothing constants from .1


    Range("N11") = "0.1"
    Range("N12") = "0.1"
    Range("AI11") = "0.1"
    Range("AO11") = "0.1"
    Range("AZ11") = "0.1"
    Range("AZ12") = "0.1"
    Range("AZ13") = "0.1"


    SolverReset
    SolverAdd CellRef:="N11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="N12", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AI11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AO11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AZ11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AZ12", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AZ13", Relation:=1, FormulaText:="=1"
    SolverOk SetCell:="AZ20", MaxMinVal:=2, ValueOf:=0, ByChange:="AZ11:AZ13", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True
    SolverOk SetCell:="AO20", MaxMinVal:=2, ValueOf:=0, ByChange:="AO11", Engine _
        :=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True
    SolverOk SetCell:="AI20", MaxMinVal:=2, ValueOf:=0, ByChange:="AI11", Engine _
        :=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True
    SolverOk SetCell:="N20", MaxMinVal:=2, ValueOf:=0, ByChange:="N11:N12", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True


    Next
End Sub

2.
Code:
Sub CopyPasteBacktestMape()

'  CopyPasteBacktestMape Macro
' Copy and paste values of backtest results between backtest and final solver. This ensures visibility of the backtest MAPE after the final solver.


Sheetlist = Array("Total_SanDiego", "Total_Ventura") 


For i = LBound(Sheetlist) To UBound(Sheetlist)


    Range("I18:AZ18").Value = Range("I21:AZ21").Value
    
    Next
End Sub
3.
Code:
Sub SolverLoopFinal()


Sheetlist = Array("Total_SanDiego", "Total_Ventura") ', "Total_SealBeach", "Total_Ventura", "San_Diego", "Miramar", "SCI", "China_Lake", "El_Centro", "Fallon", "Lemoore", "Monterey", "SNI", "COGEN")
For i = LBound(Sheetlist) To UBound(Sheetlist)


' These locations are excluded from the solver to reduce runtime, they are included in Total_SealBeach and Total_Ventura
'"NORCO", "Fallbrook","Seal_Beach", "Port_Hueneme", "Pt_Mugu"


Worksheets(Sheetlist(i)).Activate


'Initiate smoothing constants from .1


    Range("N11") = "0.1"
    Range("N12") = "0.1"
    Range("AI11") = "0.1"
    Range("AO11") = "0.1"
    Range("AZ11") = "0.1"
    Range("AZ12") = "0.1"
    Range("AZ13") = "0.1"


    SolverReset
    SolverAdd CellRef:="N11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="N12", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AI11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AO11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AZ11", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AZ12", Relation:=1, FormulaText:="=1"
    SolverAdd CellRef:="AZ13", Relation:=1, FormulaText:="=1"
    SolverOk SetCell:="AZ15", MaxMinVal:=2, ValueOf:=0, ByChange:="AZ11:AZ13", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True
    SolverOk SetCell:="AO15", MaxMinVal:=2, ValueOf:=0, ByChange:="AO11", Engine _
        :=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True
    SolverOk SetCell:="AI15", MaxMinVal:=2, ValueOf:=0, ByChange:="AI11", Engine _
        :=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True
    SolverOk SetCell:="N15", MaxMinVal:=2, ValueOf:=0, ByChange:="N11:N12", _
        Engine:=1, EngineDesc:="GRG Nonlinear"
    SolverSolve True


    Next
End Sub
 
Last edited by a moderator:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,224,862
Messages
6,181,461
Members
453,043
Latest member
Sronquest

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