Angus Spiteri
New Member
Hi
I need your expert help to have VBA coded in such a way Solver looping through all the rows in table tblProducts of the attached "Forecast" sheet.
The solver parameters should be as follows:
a) Minimise each value in a calculated field within the tblProducts table
b) By Changing each value in column tblProducts[Alpha]
c) Constraints: Value in column tblProducts[Alpha] should have the following constraints : >= 0 and <=1
My current code is quite inefficient and take a considerable amount of time to solve the value for one product.
Finally, ideally I would like to have the criteria in field tblProducts[Alpha] change to column tblProducts[Beta] or tblProducts[Gamma] based on the value in G26 of the same sheet (similar to what I have done in the sample code below)
Thank you in advance for your help.
I need your expert help to have VBA coded in such a way Solver looping through all the rows in table tblProducts of the attached "Forecast" sheet.
The solver parameters should be as follows:
a) Minimise each value in a calculated field within the tblProducts table
b) By Changing each value in column tblProducts[Alpha]
c) Constraints: Value in column tblProducts[Alpha] should have the following constraints : >= 0 and <=1
My current code is quite inefficient and take a considerable amount of time to solve the value for one product.
Finally, ideally I would like to have the criteria in field tblProducts[Alpha] change to column tblProducts[Beta] or tblProducts[Gamma] based on the value in G26 of the same sheet (similar to what I have done in the sample code below)
Code:
Sub Solver_HW()
'
' Solver_HW Macro
'
'
Dim dblMeanAbsoluteDeviationHW As Double
dblMeanAbsoluteDeviationHW = WorksheetFunction.Sum(Range("tblForecast[ABS Error - HW]"))
If Range("varHWVariableSelect") = "Alpha" Then
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Alpha]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverAdd CellRef:=Range("tblProducts[Alpha]"), Relation:=1, FormulaText:="1"
SolverAdd CellRef:=Range("tblProducts[Alpha]"), Relation:=3, FormulaText:="0"
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Alpha]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Alpha]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverSolve
End If
If Range("varHWVariableSelect") = "Beta" Then
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Beta]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverAdd CellRef:=Range("tblProducts[Beta]"), Relation:=1, FormulaText:="1"
SolverAdd CellRef:=Range("tblProducts[Beta]"), Relation:=3, FormulaText:="0"
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Beta]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Beta]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverSolve
End If
If Range("varHWVariableSelect") = "Gamma" Then
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Gamma]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverAdd CellRef:=Range("tblProducts[Gamma]"), Relation:=1, FormulaText:="1"
SolverAdd CellRef:=Range("tblProducts[Gamma]"), Relation:=3, FormulaText:="0"
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Gamma]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverOk SetCell:=dblMeanAbsoluteDeviationHW, MaxMinVal:=2, ValueOf:=0, ByChange:=Range("tblProducts[Gamma]"), _
Engine:=1, EngineDesc:="Evolutionary"
SolverSolve
End If
End Sub
Thank you in advance for your help.