Sorting A Worksheet using VBA

Hobolord

Board Regular
Joined
Sep 9, 2015
Messages
64
Hello,

I am using Excel 2013 on Windows 7 on a PC.

I am attempting to sort a worksheet on several columns in my code. The rest of the code is dependent on this sort being executed sucessfully.

I derived the below using the recorder, but even after a couple of modifications, the sort does not work the same through the code as when I perform it manually, and I am at a loss as to why.


Can anyone see any glaring mistakes that would cause the sort to not be performed correctly? Note: The data begins on row 2 with the headers on Row 1.

Code:
 Dim lastrow       As Long

Rows("1:1").Select
ActiveWorkbook.Worksheets("SalesRep").AutoFilter.Sort.SortFields.Clear


lastrow = Cells(Rows.Count, 1).End(xlUp).Row


Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("SalesRep").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("SalesRep").AutoFilter.Sort.SortFields.Add Key:= _
        Range("Z2:Z" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("SalesRep").AutoFilter.Sort.SortFields.Add Key:= _
        Range("W2:W" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortTextAsNumbers
    ActiveWorkbook.Worksheets("SalesRep").AutoFilter.Sort.SortFields.Add Key:= _
        Range("A2:A" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("SalesRep").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


Thanks for your time!

Hobo
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hello Joe,

Thanks for the reply.

How would I use multiple criteria for this method:


ActiveSheet.Sort.SortFields.Clear
Columns("A:AC").Sort Key1:=Range("Z2"), Order1:=xlDescending, Header:=xlYes


Thank you,

Hobo
 
Upvote 0
Got it. Thanks for the reference point.

Code:
ActiveSheet.Sort.SortFields.Clear
    Columns("A:AC").Sort Key1:=Range("Z2"), Order1:=xlAscending, Key2:=Range("W2"), Order1:=xlAscending, Key3:=Range("A2"), Order1:=xlAscending, Header:=xlYes
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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