View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_3_] Bob Phillips[_3_] is offline
external usenet poster
 
Posts: 2,420
Default faster method to delete rows

Use a filter

Sub Deletedata()
Dim wksSheet As Worksheet
Dim x As Long
Dim LastRow As Long
Dim rng As Range

Set wksSheet = ActiveSheet

With wksSheet

LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Columns(2).Insert
Set rng = Range("A1").Resize(LastRow)
rng.Offset(0, 1).Formula = "=OR(A1<1,A112)"
rng.Cells(1, 1).Offset(0, 1).Value = "tmp"
rng.Offset(0, 1).AutoFilter field:=1, Criteria1:=True
On Error Resume Next
Set rng = rng.Offset(1, 0).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then

rng.EntireRow.Delete
End If
.Columns(2).Delete
End With
End Sub

--
__________________________________
HTH

Bob

"Jake" wrote in message
...
I'm using this code to delete unwanted rows, testing column A and leaving
values between 1 and 12, in a database:
LastRow = wksSheet.Cells(Rows.Count, "A").End(xlUp).Row
For x = LastRow To 2 Step -1
' If .Cells(x, 1).Value < 1 Or .Cells(x, 1) 12 Then .Cells(x,
1).EntireRow.Delete
' Next x

it's effective, but takes a lot of time.
Is there a faster method for this?
thanks,
Jake