我在excel中创建了一个预测工作簿,我在单指数平滑计算中获得最佳alpha值时遇到了一些麻烦。我工作的表格如下所示
所有这些值的计算都发生在另一张纸上,我不会发布这些,因为我认为它们不相关。我和Solver一起使用excel插件来找到第一个产品(底漆)的最佳alpha值。我使用的求解器设置如下所示:
当我点击解决单元格AM2中的值变为最佳值时,这很好。但我无法弄清楚如何为其他产品做到这一点。因此,通过更改AM3的值并使用约束AM3< = 1和AM3> = 0,目标单元格将是AO3。在工作表的最终版本中将有超过700个产品。我尝试在Google上搜索但无法找到我要查找的内容。
我正在考虑创建一个宏来执行此操作,但我对VBA的了解非常有限。
答案 0 :(得分:2)
感谢@Oliver Carr提供的提示,我修复了它。希望它对未来的人有用。
Sub solverMacro()
'
' solverMarco Macro
'
Dim sheetName As String
Dim endRow As Integer
Dim row As Integer
sheetName = "SAP"
With Sheets(sheetName)
endRow = .UsedRange.SpecialCells(xlCellTypeLastCell).row 'gets the last used row
For row = 2 To endRow - 1 Step 1 'This loops through all rows that are being used
SolverReset 'resets the solver
SolverOk SetCell:="$AO$" & row, MaxMinVal:=2, ValueOf:=0, ByChange:="$AM$" & row, Engine _
:=1, EngineDesc:="GRG Nonlinear" 'Sets the target set AO + the value of row. I did this so it will increment
SolverAdd CellRef:="$AM$" & row, Relation:=1, FormulaText:="1" 'adds the constraint. Cel AM + row smaller or equal to 1
SolverAdd CellRef:="$AM$" & row, Relation:=3, FormulaText:="0" 'adds the constraint. Cel AM + row greater or equal to 0
SolverSolve True 'This runs the solver. True is there to make sure the dialog window does not show up
Next row 'Goes to the next row to start all over
MsgBox "Value is optimised" 'A msgBox to show that the macro is completed
End With
End Sub
希望这有帮助,祝你好运!