Vba sort code

bflan0524

Board Regular
Joined
Oct 7, 2016
Messages
193
Office Version
  1. 2010
if i had a sheet called 'Results By Customer' and column AK was called % Change. i wanted to build a macro then when you pressed the form it would filter that column by the top 10 customers by that criteria, i would also do another one for the bottom ten

ty
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
This code should be placed in a standard module in the workbook that contains the target worksheet.
Add a form with 2 buttons that call each of the first 2 subs or put those buttons on a worksheet.

Code:
Option Explicit

Sub ShowAKTopTen()
    SortAK
    Worksheets("Results By Customer").Range("A1").CurrentRegion.AutoFilter Field:=37, Criteria1:="10", _
        Operator:=xlTop10Items
End Sub

Sub ShowAKBottomTen()
    SortAK
    Worksheets("Results By Customer").Range("A1").CurrentRegion.AutoFilter Field:=37, Criteria1:="10", _
        Operator:=xlBottom10Items
End Sub

Sub SortAK()

    With Worksheets("Results By Customer")
    
        .AutoFilterMode = False
        .Range("A1").CurrentRegion.AutoFilter
        .AutoFilter.Sort.SortFields.Clear
        .AutoFilter.Sort.SortFields. _
            Add Key:=Intersect(.UsedRange, .Range("AK:AK")), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortTextAsNumbers
        With .AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,893
Messages
6,181,616
Members
453,057
Latest member
LE102024

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