在VBA中使用求解器excel可以在很多行中find“alpha”的最佳值和具体值

我在Excel中创build了一个预测工作簿,并且在单一指数平滑计算中遇到了一些问题,并获得了最佳的alpha值。 我工作的表单如下所示。 我正在工作的工作簿

所有这些价值的计算发生在不同的工作表,我不会发表这些,因为我认为他们是不相关的。 我用Solver玩弄了excel插件来find第一个产品(引擎)的最佳alpha值。 我使用的解算器设置如下所示: 求解器加载项设置

当我单击求解时,单元格AM2中的值变为最佳值,这很好。 但我无法弄清楚如何为其他产品做到这一点。 因此,通过改变AM3的值并使用约束条件AM3 <= 1和AM3> = 0,目标单元格将为AO3。在表单的最终版本中,将有超过700个产品。 我试图search谷歌,但无法find我在找什么。 我希望有人能帮助我。

我正在考虑创build一个macros来做这件事,但是我对VBA的知识非常有限。 如果需要更多的信息,请让我知道,我会确保发布。

预先感谢您的时间和精力!

J. Rommers

感谢@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 

希望这有帮助,祝你好运!