无法在宏

时间:2016-02-19 00:52:09

标签: excel vba excel-vba

我是VBA的新手,只知道通常做简单的事情。我已经搜索了答案并获得了线索,但我写的代码看起来很简单,应该很容易修复。

我在工作表中为一行编写了显式代码:每次都能正常工作。问题是我有数千行,我想在很多行中使用相同的求解器函数进行迭代,但它不起作用。

以下是有效的代码和不具备的代码。任何获得迭代解决方案的帮助都将非常受欢迎。

Sub Multiple_Solver()

Dim Fit_Value As Double
Dim Count1 As Integer


Count1 = 3
'solve for MA0
Fit_Value = Cells(Count1, 18)
Cells(Count1, 23) = 5 'this is $W$3 forced to an initial value
'explicit code that works follows
Application.Run "SolverReset"
Application.Run "SolverOk", "$M$3", 3, Fit_Value, "$W$3"
Application.Run "SolverSolve", True

'''''''''''&#39 ;''''''''''''& #39;'''''''''''&#39 ;''''''''''''& #39;'''''''''''&#39 ;''

'This Code DOESN'T work..... not sure why; tried to make it the same structure.
'set up variables needed
Dim Fit_Value As Double
Dim Count1 As Integer

Dim MAx_eqn, FinalZ As Range

'loop to populate many rows
For Count1 = 3 To 3 'number of solutions starting at row 3; 
'solve for MA0:
Set MAx_eqn = Cells(Count1, 13) 'this is cell with equation MA0
Fit_Value0 = Cells(Count1, 18) 'this is the retest value MA0
Set FinalZ = Cells(Count1, 23) 'this is the initial/final step value for MA0
Cells(Count1, 23) = 5 'force initial value
SolverReset
SolverOk SetCell:=MAx_eqn, MaxMinVal:=3, ValueOf:=Fit_Value0,   ByChange:=FinalZ
SolverSolve UserFinish:=True

Next Count1



End Sub

1 个答案:

答案 0 :(得分:0)

好的 - 也许这会帮助别人。我在另一个论坛上发现了这个,并且它有效。

Sub Solve_Cells()
'Run interations of solvers on many rows
Dim Count1 As Long
Dim rng As Range
Dim Fit_Value As Double

Count1 = 3
For Each rng In Range("A3:A4") 'explicit for first two rows; can fix later to bring in carrier of number of rows
    Fit_Value = Cells(Count1, 18) ' load retest value to solve for from spreadsheet
    Cells(Count1, 23) = 5 ' force to solve on negative side of curve with reset initial value
    Application.Run "SolverReset"
    Application.Run "SolverOk", rng.Offset(0, 12).Address, 3, Fit_Value, rng.Offset(0, 22).Address
    Application.Run "SolverSolve", True
    Count1 = Count1 + 1
    Next rng

End Sub
相关问题