filtering in VBA is not working for me..

bucci35

Active Member
Joined
Jul 6, 2002
Messages
341
Office Version
  1. 365
Platform
  1. Windows
HI,

I'm trying to filter with a macro but get stuck in the criterial part.
in sheet3 cell x1 I have "950 Seafood". This is what I want to filter by in sheet 4

Range("A6").Select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=Sheets("3").[x1].Value

The above formula doesn't seem to work, any ideas??
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
how about;
Code:
Range("A6:A"&rows.count).Select 
Selection.AutoFilter 
Selection.AutoFilter Field:=1, Criteria1:=Sheets("Sheet3").[x1].Value
 
Upvote 0
Hi

You can also try:

Code:
Range("A6").AutoFilter Field:=1, Criteria1:=Sheets("Sheet3").Range("X1").Value

Hope this helps
PGC
 
Upvote 0
PGC -
for some reason, your code will not work for these data layout.
Book1
ABCD
1
2
3
4
5
6
7950 Seafood
8
9
10
11
12950 Seafood
13
14
15
16950 Seafood
17
18
19
20950 Seafood
Sheet4
 
Upvote 0
Hi agihcam

I defined the range for the autofilter using a cell. Autofilter will automatically extend the range down in the current area.

Since you have empty cells in the range you want to autofilter, I would use:

Code:
Range("A6", Range("A" & Rows.Count).End(xlUp)).AutoFilter _
          Field:=1, Criteria1:=Sheets("Sheet3").Range("X1").Value

Thanks for pointing this case out.

Kind regards
PGC
 
Upvote 0
filter by blanks in macro won't work

Thanks, that helped al lot.

But, I'm using the formula below in a macro and its suppose to filter by non blanks. It won't work, any ideas


Selection.AutoFilter Field:=1, Criteria1:="<>"
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

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