Excel macro to do a specific sort?
I need to select a range of rows and sort that range by column D. How can I assign this task to a macro (so I can do this to lots of different ranges of rows开发者_JS百科 as needed)?
If you use the macro recorder in Excel to do a sort by range then you get the following code. Replace "Selection" with "Sheet1.Range("A1:D35"), or whatever your range to sort might be, and then change Key1 to whatever column you'd like to sort by. You can also just leave "Selection" in if you want to manually pick whatever you want to be sorted by the code.
Sub SortSomeStuff()
Selection.Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub
精彩评论