LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default How to make loop code more efficient

I think I interpreted your initial conditions correctly (first data row is
15, last row to delete up to is found on the "variables" sheet in B2). If
so, then see if this code is any faster than what you are running now...

Sub RemoveRows()
Dim X As Long
Dim xStop As Long
Dim OriginalCalculationMode As Long
Dim RowsToDelete As Range
Const xR As Long = 15
Const xCw As String = "S"
Const xCA As String = "T"
On Error GoTo Whoops
OriginalCalculationMode = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
With Workbooks("pipeline reporting.xls")
xStop = .Worksheets("variables").Cells(2, 2).Value + 12
With .Worksheets("Pipeline")
xStop = .Cells(.Rows.Count, 1).End(xlUp).Row
For X = xStop To xR Step -1
If .Cells(X, xCw).Value = "Diary - NTU" And _
.Cells(X, xCA).Value = "Not Taken Up" Then
If RowsToDelete Is Nothing Then
Set RowsToDelete = .Cells(X, xCw)
Else
Set RowsToDelete = Union(RowsToDelete, .Cells(X, xCw))
End If
If RowsToDelete.Areas.Count 100 Then
RowsToDelete.EntireRow.Delete xlShiftUp
Set RowsToDelete = Nothing
End If
End If
Next
End With
End With
If Not RowsToDelete Is Nothing Then
RowsToDelete.EntireRow.Delete xlShiftUp
End If
Whoops:
Application.Calculation = OriginalCalculationMode
Application.ScreenUpdating = True
MsgBox "Ended"
End Sub

--
Rick (MVP - Excel)


"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




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Efficient If's loop אלי Excel Programming 1 February 25th 09 08:49 PM
here is my code...it's running a bit slow though..any ideas to make it more efficient? thanks Zarlot531 Excel Programming 2 April 30th 07 02:12 PM
How can I make this code more efficient? Sethaholic[_27_] Excel Programming 1 August 1st 06 05:15 PM
need to make code more efficient (if possible) Lilivati Excel Programming 8 July 7th 06 07:50 PM
Loop more efficient ? farmer[_2_] Excel Programming 4 June 21st 04 06:51 AM


All times are GMT +1. The time now is 06:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright 2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"