vba 使用excel vba宏对单列中的单元格范围进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41124540/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:42:06  来源:igfitidea点击:

Sort range of cells in single column using excel vba macro

vbaexcel-vbaexcel

提问by PIYUSH VAID

I want to sort a range of cells using Excel VBA macro

我想使用 Excel VBA 宏对一系列单元格进行排序

I have searched many websites but everywhere the examples show how to sort multiple columns with a single key

我搜索了许多网站,但到处都有示例显示如何使用单个键对多个列进行排序

e.g. this example

例如这个例子

Example Excel

示例 Excel

In example above I want to sort cells A2 to A8, that's it. For this my code is

在上面的示例中,我想对单元格 A2 到 A8 进行排序,就是这样。为此,我的代码是

Range("A2:A8").Sort

But it is giving Runtime error 1004

但它给出了运行时错误 1004

What is the correct way to do it?

正确的做法是什么?

回答by Mick17

Try this

尝试这个

sub test()
Sheets("sheet1").Range("A2:A8").sort key1:=Range("A2:A8"), order1:=xlAscending, Header:=xlNo
End sub