当我运行以下代码时,Name argument not found
将出现错误“SetCell:="$I$3"
”。
Sub Simple_Exponential_Smoothing()
Dim x As Integer
Dim lastrow As Integer
Application.ScreenUpdating = False
Sheets("COV_0.2").Cells(1, 49).Value = "MAD"
Sheets("COV_0.2").Cells(1, 50).Value = "Alpha"
lastrow = Sheets("COV_0.2").Cells(Rows.Count, 2).End(xlUp).Row
For x = 2 To lastrow
If Sheets("COV_0.2").Cells(x, 31).Value <> "S4" Then
Range("G" & x & ":AD" & x).Select
Selection.Copy
Sheets("Analysis_Constant").Select
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True
Application.Run "Solver.xlam!SolverReset"
'***ERROR ON THE FOLLOWING LINE: `Name argument not found`
Application.Run "Solver.xlam!SolverOk", SetCell:="$I$3", _
MaxMinVal:=2, ValueOf:=0, ByChange:="$G$1", Engine:=3, _
EngineDesc:="Evolutionary"
Application.Run "Solver.xlam!SolverAdd", CellRef:="$G$1", _
Relation:=1, FormulaText:="0.9"
Application.Run "Solver.xlam!SolverAdd", CellRef:="$G$1", _
Relation:=3, FormulaText:="0.1"
SolverSolve True
答案 0 :(得分:0)
您必须在工具/参考中设置对Solver插件的引用:
然后正确的Solver语法是:
SolverReset
SolverOk SetCell:="$I$3", _
MaxMinVal:=2, ValueOf:=0, ByChange:="$G$1", Engine:=3, _
EngineDesc:="Evolutionary"
SolverAdd CellRef:="$G$1", Relation:=1, FormulaText:="0.9"
SolverAdd CellRef:="$G$1", Relation:=3, FormulaText:="0.1"
SolverSolve True
此外,我建议不要使用Integer
进行行计数,因为Excel的行数多于Integer
可以处理的行数。建议在VBA中使用always to use Long instead of Integer。
您可能希望了解How to avoid using Select in Excel VBA,以使您的代码更可靠,更短,更稳定,更快。