Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Help with filter Range & delete rows

Hi, i have code below that filters field 10 and then deletes the
filterd rows that are showing; this works great, but i have to filter
another field afterwards and have tried to use the same code, but it
then deletes everything ??
Can somebody tell me why..

Public Sub FilterCreatedBy()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Set WB = ActiveWorkbook
Set SH = ActiveSheet
Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=10, Criteria1:="=*za-t-m-**",
Operator:=xlAnd
On Error Resume Next
Set Rng = SH.AutoFilter.Range
Set Rng = Rng.Offset(1).Resize(Rng.Rows.Count - 1)
Set Rng = Rng.SpecialCells(xlVisible)
On Error GoTo 0
If Not Rng Is Nothing Then
Rng.EntireRow.Delete
End If
Selection.AutoFilter Field:=10
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 219
Default Help with filter Range & delete rows

This worked for me. I added column and criteria parameters to
'FilterCreatedBy' as well as a couple of other 'maintenance' stuff at
exit_Sub:
I then created a 'master' sub called TestSub and called 'FilterCreatedBy'
from it using the column and criteria parameters (see below). FYI, you can
make the 'FilterCreatedBy' Private with this method as long as you keep
'TestSub' in the same module.

'/==============================================/
Public Sub TestSub()
Call FilterCreatedBy(10, "=*za-t-m-**")
Call FilterCreatedBy(2, "*a*")
Call FilterCreatedBy(3, 20)
End Sub

'/==============================================/
Public Sub FilterCreatedBy(iColumn As Integer, varCriteria As Variant)
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range

Set WB = ActiveWorkbook
Set SH = ActiveSheet

Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=iColumn, Criteria1:=varCriteria

On Error Resume Next

Set Rng = SH.AutoFilter.Range
Set Rng = Rng.Offset(1).Resize(Rng.Rows.Count - 1)
Set Rng = Rng.SpecialCells(xlVisible)

On Error GoTo 0

If Not Rng Is Nothing Then
Rng.EntireRow.Delete
End If

exit_Sub:
On Error Resume Next
Selection.AutoFilter
Set Rng = Nothing
Range("A1").Select
Exit Sub

End Sub
'/==============================================/

HTH,
--
Gary Brown

If this post was helpful, please click the ''Yes'' button next to ''Was this
Post Helpfull to you?''.


"Les" wrote:

Hi, i have code below that filters field 10 and then deletes the
filterd rows that are showing; this works great, but i have to filter
another field afterwards and have tried to use the same code, but it
then deletes everything ??
Can somebody tell me why..

Public Sub FilterCreatedBy()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Set WB = ActiveWorkbook
Set SH = ActiveSheet
Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=10, Criteria1:="=*za-t-m-**",
Operator:=xlAnd
On Error Resume Next
Set Rng = SH.AutoFilter.Range
Set Rng = Rng.Offset(1).Resize(Rng.Rows.Count - 1)
Set Rng = Rng.SpecialCells(xlVisible)
On Error GoTo 0
If Not Rng Is Nothing Then
Rng.EntireRow.Delete
End If
Selection.AutoFilter Field:=10
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Help with filter Range & delete rows

Thanks very much for the help Gary


Gary L Brown wrote:
This worked for me. I added column and criteria parameters to
'FilterCreatedBy' as well as a couple of other 'maintenance' stuff at
exit_Sub:
I then created a 'master' sub called TestSub and called 'FilterCreatedBy'
from it using the column and criteria parameters (see below). FYI, you can
make the 'FilterCreatedBy' Private with this method as long as you keep
'TestSub' in the same module.

'/==============================================/
Public Sub TestSub()
Call FilterCreatedBy(10, "=*za-t-m-**")
Call FilterCreatedBy(2, "*a*")
Call FilterCreatedBy(3, 20)
End Sub

'/==============================================/
Public Sub FilterCreatedBy(iColumn As Integer, varCriteria As Variant)
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range

Set WB = ActiveWorkbook
Set SH = ActiveSheet

Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=iColumn, Criteria1:=varCriteria

On Error Resume Next

Set Rng = SH.AutoFilter.Range
Set Rng = Rng.Offset(1).Resize(Rng.Rows.Count - 1)
Set Rng = Rng.SpecialCells(xlVisible)

On Error GoTo 0

If Not Rng Is Nothing Then
Rng.EntireRow.Delete
End If

exit_Sub:
On Error Resume Next
Selection.AutoFilter
Set Rng = Nothing
Range("A1").Select
Exit Sub

End Sub
'/==============================================/

HTH,
--
Gary Brown

If this post was helpful, please click the ''Yes'' button next to ''Was this
Post Helpfull to you?''.


"Les" wrote:

Hi, i have code below that filters field 10 and then deletes the
filterd rows that are showing; this works great, but i have to filter
another field afterwards and have tried to use the same code, but it
then deletes everything ??
Can somebody tell me why..

Public Sub FilterCreatedBy()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Set WB = ActiveWorkbook
Set SH = ActiveSheet
Rows("1:1").Select
Selection.AutoFilter
Selection.AutoFilter Field:=10, Criteria1:="=*za-t-m-**",
Operator:=xlAnd
On Error Resume Next
Set Rng = SH.AutoFilter.Range
Set Rng = Rng.Offset(1).Resize(Rng.Rows.Count - 1)
Set Rng = Rng.SpecialCells(xlVisible)
On Error GoTo 0
If Not Rng Is Nothing Then
Rng.EntireRow.Delete
End If
Selection.AutoFilter Field:=10
End Sub



Reply
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
Delete Rows Filtered within Auto Filter Gordon[_2_] Excel Programming 1 September 30th 06 10:38 AM
filter and delete rows based on two criteria Arnold Klapheck Excel Programming 0 September 1st 06 08:13 PM
Delete rows in filter view Dakota Excel Worksheet Functions 1 March 22nd 06 02:50 PM
macro to delete duplicate rows using Filter Lost in Alabama Excel Programming 2 March 8th 06 01:30 PM
Auto Filter Delete Rows by Criteria Doesn't Work Range To Complicated robertjtucker[_8_] Excel Programming 1 September 29th 05 05:47 PM


All times are GMT +1. The time now is 07:14 PM.

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

About Us

"It's about Microsoft Excel"