发布时间 : 星期日 文章Microsoft Excel 规划求解的说明更新完毕开始阅读8007c05f312b3169a451a4d7
? 它会设置一个模型,通过更改单元格 A 1 的值会解决 50 的值的单元格 A 2 的值。 ? 它求解该模型。
? 而不显示 规划求解结果 对话框,它将最终结果保存到工作表中。
以下简单的宏创建 Microsoft Excel 规划求解模型,并没有任何用户干预的情况下解决它。 以下代码描述 Find_Square_Root 宏:
Sub Find_Square_Root()
' Set up the parameters for the model.
' Set the target cell A2 to a value of 50 by changing cell A1. SolverOK SetCell:=Range(\ ByChange:=Range(\
' Solve the model but do not display the Solver Results dialog box.
SolverSolve UserFinish:=True
' Finish and keep the final results. SolverFinish KeepFinal:=1
End Sub
SolverFinish KeepFinalVariant: = 1 End Sub Find_Square_Root2 宏,
Find_Square_Root 宏的修改的版本。 如果使用 InputBox 函数, Find_Square_Root2 宏将提示您为您要在其中解决目标单元格的值。 输入值后, Find_Square_Root2 宏将此参数设置为 SolverOK valueof 参数的值,解决问题、 将结果保存在变量数据平方根丢弃该解决方案然后将工作表中的值恢复其原始状态。 基本, Find_Square_Root2 宏说明了如何您可以将结果保存在一个或多个变量并且然后将这些可变单元格还原到其原始值。
以下代码描述 Find_Square_Root2 宏:
Sub Find_Square_Root2()
Dim val Dim sqroot
' Request the value for which you want to obtain the square root. val = Application.InputBox( _
prompt:=\ \
' Set up the parameters for the model.
SolverOK SetCell:=Range(\ ByChange:=Range(\
' Do not display the Solver Results dialog box. SolverSolve UserFinish:=True
' Save the value of cell A1 (the changing cell) before you discard
' the results.
sqroot = Range(\
' Finish and discard the results. SolverFinish KeepFinal:=2
' Show the result in a message box.
MsgBox \\
End Sub
回到顶端
如何生成报告的解决方案
Microsoft Excel 规划求解提供了多种描述结果更改的信息以及如何关闭约束的报告类型是为其关键值。 每个报表将放在单独的工作表在工作簿中上。 以下这些是 Microsoft Excel 规划求解所提供的报告类型:
? 答案报告 -The 答案报告列出目标单元格和可变单元格的其对应的原始和最终值、 约束和
约束的信息。
? 敏感度报告 -The Sensitivity 报告提供有关如何敏感解决方案是小写更改目标单元格公式
的信息。
? 限制报告 -The 限制报告列出目标单元格和可变单元格及其相应值、 和下限的限制和目标
值。
若要创建您的模型的报表,指定为 ReportArray 参数 SolverFinish 函数的值的数组。 有关 ReportArray 参数的详细信息,请参阅\SolverFinish (KeepFinal, ReportArray) 部分。 是例如如果要生成 Find_Square_Root2 宏创建并解决了模型的限制报表,修改在宏中 SolverFinish 函数,使它类似于下面的示例代码:
SolverFinish KeepFinal:=2, ReportArray:= Array(3)
到生成多个报表,修改 SolverFinish 函数,以便它类似于下面的示例代码:
SolverFinish KeepFinal:=2, ReportArray:= Array(1,2)
回到顶端
如何在循环的宏中使用 Microsoft ExcelSolver 函数
在很多的情况下很好解决目标单元格的多个值的 Microsoft Excel 规划求解。 您通常可以完成这通过使用可用于 VBA 循环结构之一。
Create_Square_Root_Table 宏演示如何将 Microsoft Excel 规划求解函数循环宏中。 Create_Square_Root_Table 宏在新工作表中创建一个表格。 它通过 10 和对应的每个数字的平方根插入一个数字。 Create_Square_Root_Table 宏创建表使用 For 循环,循环访问数字 1 到 10,并解决正方形根模型值相匹配的迭代中目标单元格。 以下代码描述 Create_Square_Root_Table 宏:
Sub Create_Square_Root_Table()
' Add a new worksheet to the workbook. Set w = Worksheets.Add
' Put the value 2 in cell C1 and the formula =C1^2 in cell C2. w.Range(\
w.Range(\
' A loop that will make 10 iterations, starting with the number 1,
' and finishing at the number 10. For i = 1 To 10
' Set the Solver parameters that indicate that Solver should ' solve the cell C2 for the value of i (where i is the number ' of the iteration) by changing cell C1.
SolverOk SetCell:=Range(\ MaxMinVal:=3, ValueOf:=i
' Do not display the Solver Results dialog box. SolverSolve UserFinish:=True
' Save the value of i in column A and the results of the ' changing cell in column B.
w.Cells(i, 1) = i
w.Cells(i, 2) = Range(\
' Finish and discard the final results. SolverFinish KeepFinal:=2
Next
' Clear the range C1:C2 w.Range(\
End Sub
SolverFinish KeepFinalVariant: = 2 下, 一步清除区域 C1:C2 w.range(\Sub Create_Square_Root_Table 宏生成图 4 所示的表。
图 4。 由 Create_Square_Root_Table 宏生成的输出
回到顶端
如何使用约束
约束是一个或多个单元格的内容限制。 一种模型可以具有一个或多个的限制。 约束集是一组 inequalities 或一组解决方案中删除的决策变量的值的某些组合的 equalities。 是例如约束可能需要一个单元格是大于零的数,以及另一个单元格只包含整数值。
我们已讨论了到目前为止的平方根模型是一个简单的模型,不包含任何约束。 图 5 说明了使用约束的模型。 此模型旨在找到最大的利润的最佳组合的产品。
图 5。 与削减 Profit Margin 的产品组合