|
|
|||
Sorting an Excel worksheet using VBScript
How can I use VBScript to sort an Excel worksheet in descending order? I can sort in ascending order because that is the default using range.Sort, but using the Excel constant xlDescending to sort in descending order results in an error.
2 Replies
Hi Lawrence,
If you can't get your VBA to work, I recommend recording a macro, sorting the range in descending order, and using that code. I put data in the range A1:A5 and recorded this macro: Sub Macro1() ' ' Macro1 Macro ' ' ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _ SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("A1:A5") .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub
Have you ever considered sorting excel worksheets by using an available tool? Kutools for Excel is just that kind of application helping you with most of the frastrating work in Excel. It provides a function to sort multiple workbook with alphabetical or numeric order in one click. To get descending order is also very simple. The add-ins collection do save much time in your work.
|
|||
|