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.
Thanks for your time!
Hobo
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