我是VBA的新手,我正在尝试开发一个宏来同时将Solver应用于多行。我能够开发一个基于教程的脚本,如果只输入一行就可以很好地工作,但是如果我指定了一个范围,那么脚本就会陷入困境,并且设置问题......"并且从未实际执行过。有人可以查看我的脚本并建议任何可能使其更好地运行的更改。另一种方法是一次一行地运行脚本超过1000行。
Sub SolverItter()
Application.Calculation = xlAutomatic
Solverreset
SolverOK setcell:="$G$42:$G$45", MaxMinVal:=2, ByChange:="$k$42:$K$45", engine:=1, enginedesc:="GRG Nonlinear"
SolverAdd CellRef:="$I$42:$I$45", Relation:=1, FormulaText:="0.3"
SolverAdd CellRef:="$I$42:$I$45", Relation:=3, FormulaText:="0.24"
solversolve userfinish:=True
End Sub
更新(9Feb2018 2:10 GMT):
我已尝试根据nbayly对EricBayes's question的回复,将我的脚本调整为For语句。这纠正了永久性的设置问题......"我遇到的问题,但现在脚本无法通过更改"来更改"的值。细胞。下面的脚本似乎更接近我的问题的答案,但仍然没有工作。
Sub SolverItter()
Dim rngObjectCells As Range
Set rngObjectCells = Range("$G$42:$G$61")
Dim rngObjectCell As Range
For Each rngObjectCell In rngObjectCells
Solverreset
Application.Calculation = xlAutomatic
SolverOK setcell:=rngObjectCell.Address, MaxMinVal:=2, ByChange:=rngObjectCell.Offset(0, 4).Range("$k$42:$K$61"), engine:=1, enginedesc:="GRG Nonlinear"
SolverAdd CellRef:=rngObjectCell.Offset(0, 2).Address, Relation:=1, FormulaText:="0.33"
SolverAdd CellRef:=rngObjectCell.Offset(0, 2).Address, Relation:=3, FormulaText:="0.20"
SolverAdd CellRef:=rngObjectCell.Offset(0, 1).Address, Relation:=1, FormulaText:="0.15"
solversolve userfinish:=True
SolverOK setcell:=rngObjectCell.Address, MaxMinVal:=2, ByChange:=rngObjectCell.Offset(0, 4).Range("$k$42:$K$61"), engine:=1, enginedesc:="GRG Nonlinear"
Next
End Sub
答案 0 :(得分:0)
在使用我的脚本几天并获得一些VBA书之后,我已经找到了如何让这个脚本工作的原因。以下是我试图找出如何使多行解算器宏工作的人的解释。
Sub MagmaMixer()
' Change Sheet1 to the name of the excel sheet you're using
Sheets("Sheet1").Select
' Set H16 to the first cell that you want the solver to set to the given
' value
Range("H16").Select
Dim i As Integer
' Tells the For loop to run the solver for six rows beginning at H16
' Change the 6 to the number of rows you want to run solver for.
For i = 1 To 6
SolverReset
Set x = ActiveCell
' This is the nuts and bolts of the solver tool. SetCell:=ActiveCell tells
' Solver that you want to set the active cell (set in the Range step above)
' to some value. MaxMinVal:= identifies what you want to set the cell to,
' where 1 is a maximum, 2 is a minimum, and 3 is a set value (ie 5).
' ByChange:=x.Offset(0,5) tells solver to accomplish the above by changing
' the value of the cell 0 columns and 5 rows to the right of the reference
' cell (ie M16 in this example).
SolverOK SetCell:=ActiveCell, MaxMinVal:=2, _
ByChange:=x.Offset(0, 5)
' SolverAdd applies additional constraints. The cell references are kept
' relative just like above.
SolverAdd CellRef:=x.Offset(0, 4), Relation:=1, FormulaText:="0.32"
SolverAdd CellRef:=x.Offset(0, 4), Relation:=2, FormulaText:="0.22"
SolverAdd CellRef:=x.Offset(0, 1), Relation:=1, FormulaText:="0.10"
' userfinish:=True suppresses the end of solver popup windows.
solversolve userfinish:=True
' Tells the macro to move the active cell to the next row.
ActiveCell.Offset(1, 0).Select
Next i
End Sub