"Generic" Autofilter Macros Applied to Different Worksheets

ACF0303

New Member
Joined
Aug 29, 2013
Messages
41
Greetings,

I have a workbook with multiple worksheets and for each worksheet I want to be able to perform the same functions (when I am on that worksheet). The macros are pretty simple: I want to be able to sort from high to low, low to high, a to z and z to a for a selection of columns. Every worksheet will have the same column headers, although not the same number of rows of data.

My question: how can I create the macros so that I do not have to write the same ones over and over for each different sheet? Is there a way to write ones that specify the active worksheet and can adjust the range to include any rows with data in them?

Thanks for any help!

Cathy
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try recording a macro whilst doing this manually and post the recorded code in your reply.
 
Upvote 0
Sure. Here is what it looks like. Now imagine - I have a number of other worksheets that I want to apply the same macro to, but they are named differently and may use a different range (more or less rows).

Code:
Range("E1").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("SUP_TOP_BOTTOM").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("SUP_TOP_BOTTOM").AutoFilter.Sort.SortFields.Add Key:=Range _
        ("F1:F58"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("SUP_TOP_BOTTOM").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
End Sub
 
Upvote 0
Try

Code:
Dim LR As Long
LR = Range("F" & Rows.Count).End(xlUp).Row
Range("E1").Select
    Selection.AutoFilter
    ActiveSheet.AutoFilter.Sort.SortFields.Clear
    ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range _
        ("F1:F" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
 
Upvote 0

Forum statistics

Threads
1,224,888
Messages
6,181,602
Members
453,055
Latest member
cope7895

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