由於由@Oliver卡爾提供的提示我固定它。希望它對以後的某個人有用。
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
希望這會有所幫助,goodluck!
嘗試[記錄宏](https://support.office.com/en-us/article/Create-or-delete-a-macro-974ef220-f716-4e01-b015-3ea70e64937b),這將暴露解算器計算背後的VBA元素,然後您可以將它們格式化爲一個循環,以重複使用您的700種產品。 – Carrosive
@OliverCarr非常感謝您的提示。經過更多的谷歌搜索它現在按預期工作! –