View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bernie Deitrick Bernie Deitrick is offline
external usenet poster
 
Posts: 5,441
Default Deleting rows that dont fit criteria

All the previous suggestions involve stepping up column A and deleting rows one by one. A possible
faster technique is to group the rows based on your criteria, then do one deletion. The code below
is between 4 to 250 times faster than the stepping code for the specific condition of 10,000 rows
(with 1% needing to be deleted, it is 4 times faster (0.4 seconds versus 1.6 seconds), with 99%
needing to be deleted, it is 250 times faster (0.4 seconds versus 90 seconds)). (Note that it is
actually slower if no rows need to be deleted, but not noticably: 0.23 seconds vs 0.14 seconds).
Obviously, as more rows need to be checked and deleted, the run time difference increases, and will
decrease as fewer need to be deleted. If this type of row deletion is something that you use on
large files on a regular basis, then optimizing your code could be important...

HTH,
Bernie
MS Excel MVP

Sub Delete00sInColA()
Dim myRow As Long
Dim myC As Range

myRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

Range("A1").EntireColumn.Insert
Range("A1").Value = "Keep"
With Range("A2:A" & myRow)
.FormulaR1C1 = _
"=IF(RIGHT(RC[1],2)=""00"",""Trash"",""Keep"")"
.Value = .Value
End With
Cells.Sort Key1:=Range("A2"), Order1:=xlAscending
Set myC = Columns("A:A").Find(What:="Trash", After:=Range("A1"))
Range(myC, Cells(myRow, 1)).EntireRow.Delete
Range("A1").EntireColumn.Delete
End Sub


"Cuervogold" wrote in message
...
I am new to the Excel macro language, and I am trying to delete entire
rows from 10000+ data points if the last two digits of a 12-digit
number in column A do not end in 00. Here is the macro I have, but it
will only delete the cell in column A. Again, I have an entire
worksheet with corresponding data that needs to be deleted. Thanks

============================

Sub Delete_numbers()

Dim i As Long
Dim number As string
Dim last2 As string

i = 1
number = ActiveCell.FormulaR1C1
For i = 1 to 1000000
number = ActiveCell.FormulaR1C1
If Not number = "" Then
last2 = Right(number, 2)
If Not last2 = "00" Then
Selection.Delete Shift:=xlUp
Else
ActiveCell.Offset(1, 0).Range("A1").Select
End If
Else
Exit For
End If
Next i
End Sub