Autofilter on another sheet

n0gel

Board Regular
Joined
Mar 7, 2005
Messages
177
Hello forum, I'm trying to put together a macro that will run an autofilter on a range on another sheet, and return the filtered results to the sheet from which I ran the macro. So the same macro will work on all sheets without modifying the code for each sheet to reflect that sheet's name.
So I start out on sheet3 say,
then run the macro from sheet3,
it does an autofilter on sheet1 say, and copies the result back to what ever sheet I started from, in this case sheet3.
And the same macro would work if I started on sheet 20.

Thanks forum, I always appreciate your help.

edit: And the filter criteria is located in a cell on the sheets from which the macro is run and to which the data will be copied. So each sheet will have 1 cell each containing the filter criteria to use on the other sheet, or sheet 1 in this case.
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Something like this...
Rich (BB code):
Sub FilterSheet1()
'Filters sheet1 by a specific column, copies results to current sheet at A4
Dim fCol As Long    'column to filter on sheet1
Dim Crit As String  'value to filter by
Dim LR   As Long    'last row of visible filtered data

fCol = 1                            'Column A is 1, column B is 2, etc
Crit = Range("A1").Value            'uses value in A1 for the filter
Application.ScreenUpdating = False  'speed up macro

With Sheets("Sheet1")
    .AutoFilterMode = False
    .Rows(1).AutoFilter Field:=fCol, Criteria1:=Crit
    LR = .Range("A" & .Rows.Count).End(xlUp).Row
    If LR > 1 Then
        .Range("A1:A" & LR).EntireRow.Copy Range("A4")
    Else
        Range("A4") = "No results found"
    End If
    .AutoFilterMode = False
End With

Application.ScreenUpdating = True
End Sub


The part in red is the target on the activesheet, adjust that to where you want the list to display.
 
Upvote 0
Wow. As soon as I tested your code about six more uses for this came to mind.
Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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