VBA代码通过解算器拉取数据并粘贴到不同的工作表上

时间:2017-12-04 16:57:02

标签: vba excel-vba solver excel

我很难弄清楚为什么我的VBA代码不起作用(只是部分工作),并觉得它与将求解器集成到我的VBA中有关。我是Stack Overflow的新手,所以如果我没有正确发布我的问题,我会道歉。附件是我的代码的副本,以及我的三张表格的屏幕截图,其中代码接口和第三张工作表上应显示最终值的结果。

请注意,由于采取了安全措施,我的屏幕截图中的值会变黑。

随意提出任何问题以进一步澄清我的问题。

Sub WorkingLoop()
'
' WorkingLoop Macro
'
' Keyboard Shortcut: Ctrl+Shift+L
'

SolverOptions StepThru:=False

Dim i As Long
Dim LastRow As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim sht3 As Worksheet

Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Modified Data")
Set sht2 = wb.Sheets("Solver | Macro")
Set sht3 = wb.Sheets("New Price")

LastRow = sht1.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row


For i = 2 To LastRow
sht2.Range("A2") = sht1.Range("A" & i).Value
sht2.Range("F2:F7") = sht2.Range("D2:D7").Value

SolverOk SetCell:="$B$14", MaxMinVal:=1, ValueOf:=0, ByChange:="$B$12", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1

sht3.Range("A" & i) = sht2.Range("A2").Value
sht3.Range("D" & i) = sht2.Range("B10").Value
sht3.Range("E" & i) = sht2.Range("B11").Value
sht3.Range("F" & i) = sht3.Range("B17").Value
sht3.Range("G" & i) = sht3.Range("B18").Value
sht3.Range("H" & i) = sht3.Range("B19").Value
sht3.Range("I" & i) = sht3.Range("B20").Value
sht3.Range("J" & i) = sht3.Range("B21").Value
sht3.Range("K" & i) = sht3.Range("B22").Value

Next i

End Sub
  1. Sheet1:将用于在解算器中移动的值 https://i.stack.imgur.com/nhUZn.png
  2. Sheet2:ModData中的值通过解算器移动 https://i.stack.imgur.com/5KUwU.png
  3. Sheet3:在通过求解器运行后要过帐新价格的位置 https://i.stack.imgur.com/T2HGd.png

0 个答案:

没有答案