2016-10-10 29 views
0

我在Excel中創建了一個預測工作簿,並且在單指數平滑計算中獲得最佳Alpha值時遇到了一些麻煩。 im正在工作的工作表如下所示。 The workbook i'm working in在VBA中使用求解器excel在很多行中查找「alpha」的最佳值和具體值

所有這些值的計算髮生在不同的工作表中,我不會發布這些因爲我認爲他們不相關。我用Solver玩弄了excel插件以找到第一個產品(引擎)的最佳alpha值。我使用的求解器設置如下所示: The Solver add-in settings

當我單擊求解單元格AM2中的值更改爲最佳值時,這很好。但我無法弄清楚如何爲其他產品做到這一點。因此,通過更改AM3的值並使用約束AM3 < = 1和AM3> = 0,目標單元格將爲AO3。在表單的最終版本中,將有超過700個產品。我試圖在Google上搜索,但無法找到我正在尋找的內容。我希望有人能幫助我。

我想創建一個宏來做到這一點,但我對VBA的知識非常有限。如果需要其他信息,請告訴我,我會確保發佈。

在此先感謝您的時間和精力!

J. Rommers

+0

嘗試[記錄宏](https://support.office.com/en-us/article/Create-or-delete-a-macro-974ef220-f716-4e01-b015-3ea70e64937b),這將暴露解算器計算背後的VBA元素,然後您可以將它們格式化爲一個循環,以重複使用您的700種產品。 – Carrosive

+1

@OliverCarr非常感謝您的提示。經過更多的谷歌搜索它現在按預期工作! –

回答

1

由於由@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!

相關問題