vba 使用VBA进行排序操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6894752/
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
Sorting operation using VBA?
提问by niko
How do I perform a sorting operation using VBA code ? Any defined macros to perform that ?
如何使用 VBA 代码执行排序操作?任何定义的宏来执行?
I have to sort my column B in ascending order which has values in it. And C column which has dates in it and in the fashion oldest to newest.
我必须按升序对 B 列进行排序,其中包含值。和 C 列,其中包含日期和最旧到最新的时尚。
回答by aevanko
I highly reccomend you play around with the macro recorder. If you do not see a developer tab in your Excel, go to excel options and it should be in the Popular tab. Click to turn it on.
我强烈建议您使用宏记录器。如果您在 Excel 中没有看到开发人员选项卡,请转到 Excel 选项,它应该位于“流行”选项卡中。单击以将其打开。
Press Record Macro, then do the thing you want to do (like highlight a column and press Sort on the ribbon and sort by ascending) then Stop Recording. Go into VBA (Alt+F11) and see the resulting code to learn the syntax on how to write the same function.
按录制宏,然后执行您想做的操作(例如突出显示一列并按功能区上的排序并按升序排序),然后停止录制。进入 VBA (Alt+F11) 并查看生成的代码以了解如何编写相同函数的语法。
For example, sort dates will result in something like this:
例如,排序日期将导致如下结果:
Sub Macro1()
Columns("F:F").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("F1:F5"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("F1:F5")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Since you will already select the column you want to sort when you run your macro, you can clean the code up to something like this:
由于您在运行宏时已经选择了要排序的列,因此您可以将代码清理为如下所示:
Sub SortAscending()
With ActiveWorkbook.Worksheets("Sheet1").Sort
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sorting dates can be done with the same codes since it's ascending order:
排序日期可以使用相同的代码完成,因为它是升序:
The trick is to remember that you don't need to select the range in the code that you are already selecting. Good luck and have fun with the recorder, it really does help in cases like this.
诀窍是记住,您不需要在已经选择的代码中选择范围。祝你好运,享受录音机的乐趣,它在这种情况下确实有帮助。
Also, there are numerous books on Excel VBA and I reccomend you picking one up at the book store or digitally. Most books out there will walk you through the basic of understanding VBA basics, main keywords, using the recorder, then eventually far along enough so you can write your own code (recorder code is hardly optimized or perfect, but is great for learning the wording (syntax) for doing some worksheet functions).
此外,有很多关于 Excel VBA 的书籍,我建议您在书店或数字版中挑选一本。大多数书籍都会带您了解 VBA 基础知识、主要关键字、使用记录器的基础知识,然后最终足够深入,以便您可以编写自己的代码(记录器代码几乎没有优化或完美,但非常适合学习措辞(语法)用于执行一些工作表函数)。