View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default VBA Question - Is there a better approach?

Since you are using a find it will not make a substantial difference. In fact
I would be inclined to use

With .Columns("M")

Then you do not have to worry about how many cells are populated. You are
not iterating through each of the cells with a find...

If you want to get a performance improvement change the code to accumulate
all of the found ranges into one big range using the union operator and then
just do one big delete at the end. Deleting one row at a time is a
substantial drain on your resources.
--
HTH...

Jim Thomlinson


"Scott Wagner" wrote:

Below is a piece of code I use to delete rows that contain a specific keyword
in a designated column. Right now I specify the range m2:m1500, but rarely
have that many lines in the raw data. I chose the number 1500 to be sure the
code runs on all lines. Right now this is the largest time contributor to
the macro I run on the data, of which this is a small part.

Is there an approach I can follow other than the one shown below that
addresses the speed, and would also not require me to specify the range... so
it acts only the actual number of row of data in the worksheet?

Thanks,

Scott



'----------------------------------------------------
'Delete extra lines
'----------------------------------------------------
myWords = Array("2")

Set wks = ActiveSheet
With wks
With .Range("m2:m1500")
For iCtr = LBound(myWords) To UBound(myWords)
Do
Set FoundCell = .Cells.Find(What:=myWords(iCtr), _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If FoundCell Is Nothing Then
Exit Do
Else
FoundCell.EntireRow.Delete
End If
Loop
Next iCtr
End With
End With