从另一个子调用Excel解算器提供的结果与手动

时间:2016-07-05 04:20:49

标签: excel vba excel-vba solver

我需要在一个工作表中运行Solver(" Sheet1")以获取名为" air"的参数的不同值,此参数不是Solver参数的一部分,但它对结果有影响,所以我在" Sheet2"使用" air"的不同值并为每个" air"制作了一个代码来运行Solver。价值回归"恢复"一些来自Sheet1的结果并将它们放在" Sheet2"

这是我为" Sheet2"

制作的代码
Sub F1()
    Dim air() As Variant
    air = Selection.Value 'Selection of different % of "air" from a table in Sheet2
    i = UBound(air, 1) 'Length of air array
    For j = 1 To i
    Sheet1.Range("$H$35").Value = air(j, 1) 'Change parameter "air" of Sheet1
    Call Sheet1.Resolver 'Run Solver on Sheet1 to obtain new results
    ActiveCell.Offset(j - 1, 1).Value = Sheet1.Range("$P$132").Value 'Paste new result "$P$132" from Sheet1 on a cell one space right to "air" in table from Sheet2
    ActiveCell.Offset(j - 1, 2).Value = Sheet1.Range("$A$54").Value 'Paste new result "$A$54" from Sheet1 on a cell two spaces right to "air" in table from Sheet2
    ActiveCell.Offset(j - 1, 3).Value = Sheet1.Range("$P$117").Value 'Paste new result "$P$117" from Sheet1 on a cell three spaces right to "air" in table from Sheet2
    Next j  
End Sub

这是Sheet1的子解析器:

Sub Resolver()
  SolverReset
  SolverOk SetCell:=Range("$A$51"), MaxMinVal:=3, ValueOf:="0",   ByChange:=Range("$H$36:$H$38,$A$54"), Engine:=1
  SolverAdd CellRef:=Range("$A$45"), Relation:=2, FormulaText:=0
  SolverAdd CellRef:=Range("$A$47"), Relation:=2, FormulaText:=0
  SolverAdd CellRef:=Range("$A$49"), Relation:=2, FormulaText:=0
  SolverOptions AssumeNonNeg:=False
  SolverSolve UserFinish:=True
  SolverFinish KeepFinal:=1
End Sub

此代码正在运行,但如果我将它们与手动运行子解析器获得的结果进行比较,则会得到错误的值。例如:

使用第一个代码:

air      x       y         z
0,10    56,52   35,08     7.093,49
0,20    56,52   35,08     5.716,48
0,30    56,52   35,08     4.787,19
0,35    56,52   35,08     4.427,32

手动使用第二个代码:

 air    x      y       z
0,10  74,29   57,79   9.324,50
0,20  67,19   48,13   6.796,69
0,30  60,08   39,43   5.089,14
0,35  56,52   35,08   4.427,32

在第一个代码的结果中,只有最后一行是可以的,因为在运行F1之前,我手动运行Resolver,其值为" air"。如果我改变" air"的顺序它们是相同的,只有0.35行是可以的。

然后我意识到在" Sheet2"在运行F1之后,单元格的价值为$ A $ 51,$ H $ 36:$ H $ 38,$ A $ 54,$ A $ 45,$ A $ 47,$ A $ 49(同样在Resolver中使用)为0,所以现在我认为问题是解析器正在" Sheet2"而不是" Sheet1"。所以我尝试了以下内容:

Sub Resolver()
  SolverReset
  SolverOk SetCell:=Sheet1.Range("$A$51"), MaxMinVal:=3, ValueOf:="0", ByChange:=Sheet1.Range("$H$36:$H$38,$A$54"), Engine:=1
  SolverAdd CellRef:=Sheet1.Range("$A$45"), Relation:=2, FormulaText:=0
  SolverAdd CellRef:=Sheet1.Range("$A$47"), Relation:=2, FormulaText:=0
  SolverAdd CellRef:=Sheet1.Range("$A$49"), Relation:=2, FormulaText:=0
  SolverOptions AssumeNonNeg:=False
  SolverSolve UserFinish:=True
  SolverFinish KeepFinal:=1
End Sub

但是不起作用,我该如何运行" Resolver"在" Sheet1"?谢谢!

1 个答案:

答案 0 :(得分:0)

解决了,我不知道它是否是脏代码,但是正在运行。

在F1上添加以下代码:

Sheet1.Select

在致电Resolver之前。

Sheet2.Select

返回Sheet2和"粘贴"数据。

然后:

Sub F1()
On Error GoTo errHandler
Application.ScreenUpdating = False

Dim air() As Variant
air = Selection.Value 'Selection of different % of "air" from a table in Sheet2
i = UBound(air, 1) 'Length of air array

For j = 1 To i
Sheet1.Range("$H$35").Value = air(j, 1) 'Change parameter "air" of Sheet1
Sheet1.Select
Call Sheet1.Resolver 'Run solver on Sheet1 to obtain new results
Sheet2.Select
ActiveCell.Offset(j - 1, 1).Value = Sheet1.Range("$P$132").Value 'Paste new results "$P$132" from Sheet1 on a cell 1 space right to "air" in table from Sheet2
ActiveCell.Offset(j - 1, 2).Value = Sheet1.Range("$A$54").Value 'Paste new results "$A$54" from Sheet1 on a cell 2 spaces right to "air" in table from Sheet2
ActiveCell.Offset(j - 1, 3).Value = Sheet1.Range("$P$117").Value 'Paste new results "$P$117" from Sheet1 on a cell 3 spaces right to "air" in table from Sheet2
Next j

Application.ScreenUpdating = True
errHandler:
Application.ScreenUpdating = True
End Sub

使用

Sub F1()
On Error GoTo errHandler
Application.ScreenUpdating = False
....(code)...
....(code)...
Application.ScreenUpdating = True
errHandler:
Application.ScreenUpdating = True
End sub

避免更改工作表之间的闪烁。

相关问题