I have a bunch of data where I need to make adjustments to certain values using Solver. Doing this manually would take me weeks or more so I set up some looping code to run Solver within VBA. The problem is that it is verrrryy slow.
Here is my original code:
Sub Solver()
'
Application.ScreenUpdating = False
Dim i As Integer
For i = 660 To 900 'Range("A" & Rows.Count).End(xlUp).Row
If Cells(i, "P") <> "0" And Year(Cells(i, "B")) < "2006" Then
SolverReset
SolverOk SetCell:=Cells(i, "P").Address, MaxMinVal:=3, ValueOf:=0, ByChange:=Cells(i, "Q").Address, Engine:=1 _
, EngineDesc:="GRG Nonlinear"
SolverOptions MaxTime:=0, Iterations:=0, Precision:=0.001, Convergence:= _
0.001, StepThru:=False, Scaling:=False, AssumeNonNeg:=False, Derivatives:=2
SolverSolve userfinish:=True
End If
Next
Application.ScreenUpdating = True
End Sub
I was thinking that by reordering the steps, I could get the process to run much more efficiently by quickly passing over rows where no action is needed. The code below is what I came up with but it is just as slow as the original.
Sub Solver2()
'
Dim i As Integer
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
If Cells(i, "P") = "0" Or Year(Cells(i, "B")) > "2005" Then
'Do nothing
ElseIf Cells(i, "P") <> "0" And Year(Cells(i, "B")) < "2006" Then
SolverReset
SolverOk SetCell:=Cells(i, "P").Address, MaxMinVal:=3, ValueOf:=0, ByChange:=Cells(i, "Q").Address, Engine:=1 _
, EngineDesc:="GRG Nonlinear"
SolverOptions Precision:=0.001, Convergence:=0.001, AssumeNonNeg:=False
SolverSolve userfinish:=True
End If
Next
Application.ScreenUpdating = True
End Sub
How can I make this process run more quickly? Thanks!
Bookmarks