Peter
Mon Mar 17 04:23:34 CDT 2008
Hi Rob, hope you don't mind me butting in!
The Filter method is tried and tested and works well. However somewhat to my
surprise I found the routine posted by Rick worked 30-50% faster than the
filter method (% increasing with greater no. of discontinuous rows to
delete). I disabled screen updating in Rick's.
My test was based on pre-filling data in only col-A like this
Rows.Hidden = False
Columns(1).Clear
Set rng = Range("A1:A10000")
ReDim arr(1 To rng.Rows.Count, 1 To 1) As Long
For i = 1 To UBound(arr) Step 2
arr(i, 1) = 1
Next
rng.Value = arr
I only tested setting the rng to 4k & 10k cells, I didn't bother with 40k as
in another thread.
Comparative speeds may well be different with different data sets and/or
formulas (best also to disable calculation in Rick's if formulas are
included) and not least depending on the 'process', which in this case was
to delete rows.
Couple of comments about your code
It's always good to quote source for code but I'm pretty sure the 'find the
"Lastcell" settings' method was posted in this ng a long time before it may
have appeared on that url.
Although the lastcell method worked fine in my testing with data only in
col-A, typically (though not necessarily) one would want to process a
specific column, eg find zeros in col-A, not in whatever the last data
column might happen to be.
SpecialCells - need to be careful if there is any possibility of attempting
to return 8192+ discontiguous areas, it'll fail so use within a max
cell.count of 16k, if necessary do 'chunks'. However SpecialCells also gets
very slow to return a large qty of discontinuous areas, that alone might be
the reason Rick's worked faster in my testing.
.AutoFilterMode = False
You have that qualified to a range object whereas it should be to a
worksheet, suggest change to
.Parent.AutoFilterMode = False
Just to avoid any misunderstanding, I don't want to give any impression I am
discounting the filter method - I'm not, it's very useful for many purposes.
Regards,
Peter T
"broro183" <broro183@discussions.microsoft.com> wrote in message
news:12DC7B0E-4B51-485A-B055-DA97B7BB1593@microsoft.com...
> hi Rick,
>
> Since you have an example of an extreme situation would you mind doing
some
> comparison testing on the speed of my below autofilter solution?
> (it's a hybrid too - from various codes of mine)
>
> Sub HowDoesFilteringCompare()
> 'speed up macro
> With Application
> .ScreenUpdating = False
> .Calculation = xlCalculationManual
> End With
>
> Dim LastRow As Long
> Dim LastCol As Long
> Dim RowsToDelete As Range
> Dim RangeOfAutoFilter As Range
>
> Const DataStartRow As Long = 1
> Const SheetName As String = "Sheet1"
>
> With Worksheets(SheetName)
> 'find the "Lastcell" settings based on
>
http://www.beyondtechnology.com/geeks012.shtml
> On Error Resume Next ' in case there are no data cells
> LastRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious,
> SearchOrder:=xlByRows).Row
> LastCol = .Cells.Find(What:="*", SearchDirection:=xlPrevious,
> SearchOrder:=xlByColumns).Column
> On Error GoTo 0
>
> 'check if autofilters are applied, remove & reapply
> If .AutoFilterMode Then .AutoFilterMode = False
> Set RangeOfAutoFilter = .Range(.Cells(DataStartRow, 1),
> .Cells(LastRow, LastCol))
>
> With RangeOfAutoFilter
> 'filtering column A based on "If .Cells(X, 1).Value = 0"
> .AutoFilter Field:=1, Criteria1:="=0"
> 'selects & deletes all visible rows
> On Error Resume Next ' in case there are no visible cells
> Set RowsToDelete = .Offset(1, 0).Resize(.Rows.Count -
> 1).SpecialCells(xlVisible).EntireRow
> RowsToDelete.Delete
> On Error GoTo 0
> 'to remove the filters
> .AutoFilterMode = False
> End With
> End With
>
> 'free memory
> Set RowsToDelete = Nothing
> Set RangeOfAutoFilter = Nothing
>
> 'reset settings
> With Application
> .ScreenUpdating = True
> .Calculation = xlAutomatic
> End With
>
> End Sub
>
>
> Thanks
> Rob
>
> Rob Brockett
> NZ
> always learning & the best way to learn is to experience...