View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Mike[_27_] Mike[_27_] is offline
external usenet poster
 
Posts: 9
Default How to make loop code more efficient


"macroapa" wrote in message
...
Hi,

I ahve the following code that loops thru each row and if the criteria
is met it deletes the row. However it runs really slowly. Is there a
way to acheive the same thing more effeiciently? Thanks

Sub removeNTUs()

Dim xR As Long
Dim xCw As Integer
Dim xCA As Integer
Dim wStep As String
Dim AStatus As String
Dim xStop As Long


xCw = 19
xCA = 20
xStop = Workbooks("pipeline reporting.xls").Worksheets
("variables").Cells(2, 2).Value + 12
xR = 15
Do
Debug.Print (xR)
wStep = Workbooks("pipeline reporting.xls").Worksheets
("Pipeline").Cells(xR, xCw).Value
AStatus = Workbooks("pipeline reporting.xls").Worksheets
("Pipeline").Cells(xR, xCA).Value

If wStep = "Diary - NTU" And AStatus = "Not Taken Up" Then
Rows(xR & ":" & xR).Delete Shift:=xlUp
xR = xR - 1
xStop = xStop - 1
End If
xR = xR + 1
Loop Until xR = xStop
MsgBox ("ended")
End Sub


Some ideas .... make sure that screen updating is off. Also, it might be
faster if you had a formula on the worksheet which tested for your
conditions and equated to true/false and use that to delete rows. Also,
when trundling with delete I usually work from the bottom up. I don't know
if that is faster or not. Finally, a completely different approach might by
to use and advance filter (in place) based on criteria.
Mike