vba Excel宏进行特定排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4168802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel macro to do a specific sort?
提问by Daniel
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 as needed)?
我需要选择一系列行并按 D 列对该范围进行排序。如何将此任务分配给宏(以便我可以根据需要对许多不同范围的行执行此操作)?
回答by Michael
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.
如果您在 Excel 中使用宏记录器按范围进行排序,则会得到以下代码。将“Selection”替换为“Sheet1.Range("A1:D35"),或者您要排序的任何范围,然后将 Key1 更改为您想要排序的任何列。您也可以将“Selection”留在如果您想手动选择您想要按代码排序的任何内容。
Sub SortSomeStuff()
Selection.Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub