开发者

Excel macro to do a specific sort?

开发者 https://www.devze.com 2023-01-25 01:25 出处:网络
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 mac

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
0

精彩评论

暂无评论...
验证码 换一张
取 消