How do I filter in place?

HunterN

Active Member
Joined
Mar 19, 2002
Messages
479
Hi,

I have the following columns on a sheet:

A B C D
NAME SIZE LOCATION ACTION
PESAF 2 951-952 N
PESB1 3 953-955 M
PESB2 2 XXX-XXX D
PESC1 2 956-957 M


I need to filter out all the rows that have a 'D' in the Action column, using VBA.

I can't seem to get this!

I recorded a macro and it was the following:

Sub Macro1()
Range("D1").select
Range(selection, Selection.End(xlDown)).Select

Selection.AutoFilter Field:=1, Criteria1:="=M", Operator:=xlOr, _
Criteria2:="=N"
Range("A1:E1").select
End Sub

But this is not working for me in the actual program. I could use some help! :(

Thanks, Nancy
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I need to filter out all the rows that have a 'D' in the Action column, using VBA.

Hi, Nancy. Your code is written to filter for N or M in the action column. If you need to filter for D's, change

Code:
Selection.AutoFilter Field:=1, Criteria1:="=M", Operator:=xlOr, _ 
Criteria2:="=N"
to
Code:
Selection.AutoFilter Field:=1, Criteria1:="=D"

Also, you rarely need to select items in VBA to use them (and selecting them can really slow down your code). With that in mind, you can condense your code a bit to be:

Code:
Sub Macro1()
Range(Range("D1"), Range("D1").End(xlDown)).AutoFilter _
Field:=1, Criteria1:="=D"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top