advance filter with list and criteria in different sheets in the same workbook

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
784
Office Version
  1. 365
Platform
  1. Windows
Hi,

In an advance filter:
Is there a way to have sheet 1 have the list range
And sheet 2 have the criteria and extract.
OR does all three have to be in the same sheet.

I tried to have the list range (“DATA”) in sheet 1
And the criteria and extract in sheet 2 but I don’t get any results
I had to copy the range "DATA" to sheet 2 to make it work

For future…does all three have to be in the same workbook



mike
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
If you wanted to, you could even have the extract go to a third sheet. Consider the following example code:

VBA Code:
Option Explicit
Sub Advanced_Filter_Example()
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    Set ws3 = Worksheets("Sheet3")
  
    Set rngList = ws1.Range("A1").CurrentRegion     '<< the data to filter is on sheet 1
    Set rngCriteria = ws2.Range("A1").CurrentRegion '<< the criteria is on sheet 2
    Set rngCopyTo = ws3.Range("A1")                 '<< the 'extract to' range is on sheet 3
  
    ws3.Range("A1").CurrentRegion.Clear
  
    rngList.AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo

End Sub

with this on sheet 1
Book1
ABC
1header1header2header3
2adatadata
3bdatadata
4cdatadata
5adatadata
6bdatadata
7cdatadata
8
Sheet1


and this on sheet 2
Book1
AB
1header1
2a
3
Sheet2


you'll get this on sheet 3
Book1
ABC
1header1header2header3
2adatadata
3adatadata
4
Sheet3


but as far as I know, they do have to be in the same workbook (but happy to be corrected)
 
Upvote 0
Addendum:

I just tested it, and it turns out that all three can be on different workbooks as well! This code worked:

VBA Code:
Option Explicit
Sub Advanced_Filter_Example_2()
    Dim rngList As Range, rngCriteria As Range, rngCopyTo As Range
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = Workbooks("filter book 2").Worksheets("Sheet1")
    Set ws3 = Workbooks("filter book 3").Worksheets("Sheet1")
    
    Set rngList = ws1.Range("A1").CurrentRegion     '<< the data to filter is on sheet 1
    Set rngCriteria = ws2.Range("A1").CurrentRegion '<< the criteria is on sheet 2
    Set rngCopyTo = ws3.Range("A1")                 '<< the 'extract to' range is on sheet 3
    
    ws3.Range("A1").CurrentRegion.Clear
    
    rngList.AdvancedFilter xlFilterCopy, rngCriteria, rngCopyTo

End Sub

this
filter book 1.xlsm
ABC
1header1header2header3
2adatadata
3bdatadata
4cdatadata
5adatadata
6bdatadata
7cdatadata
8
Sheet1


and this
filter book 2.xlsx
AB
1header1
2a
3
Sheet1


led to this
filter book 3.xlsx
ABC
1header1header2header3
2adatadata
3adatadata
4
Sheet1
 
Upvote 0
Solution
Hi kevin9999
Thank you
It works perfect
Now I don't have to have extra columns that copies the "list" from sheet1 out of sight on sheet2 where the "extract" results is
I'm also putting your other macro in my "Macro Helper" workbook for the future.

mike
 
Upvote 0
Hi kevin9999
Thank you
It works perfect
Now I don't have to have extra columns that copies the "list" from sheet1 out of sight on sheet2 where the "extract" results is
I'm also putting your other macro in my "Macro Helper" workbook for the future.

mike
You're welcome Mike, and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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