Generalising Deleting Rows after AUTOFILTER

J

jason

I have this code:

Sub Macro2()

Range("A1").AutoFilter Field:=1, Criteria1:="b"
Rows("7:9").Delete Shift:=xlUp
Selection.AutoFilter

End Sub

and its used on a small table like this:

Name Age
a 5
c 75
c 96
c 4
c 8
b 59
b 8
b 7

I've got no way of predicting how many rows there'll be in the table,
so how do I generalise the second row of the code? i.e the bit
'Rows("7:9")'.
Or do I need to look into an alternative approach?

Any help greatly appreciated,
Jason.
 
T

Tom Ogilvy

Sub Macro2()
Dim rng As Range
Range("A1").AutoFilter Field:=1, Criteria1:="b"
Set rng = ActiveSheet.AutoFilter.Range
Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1)
rng.EntireRow.Delete
Range("A1").AutoFilter
End Sub


Will delete all the entries with b

Test on a copy of your data.
 
G

Guest

hi.
there is no way to generalize code. code is specific in
what it does.
you will have to add code that will find and select what
you wish to delete.
this line will open the find dialog box
Application.Dialogs(xlDialogFormulaFind).Show
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top