vba VBA代码按升序对Excel列进行排序并展开选择?

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

VBA Code to sort an Excel Column in Ascending Order and Expand Selection?

excelvbasortingexcel-vba

提问by JC11

I am attempting to sort column D in ascending order in an excel file with VBA. I would like to expand the selection for all values in Column A-F.

我正在尝试使用 VBA 在 Excel 文件中按升序对 D 列进行排序。我想扩展列 AF 中所有值的选择。

I've used this formula as a starting point:

我使用这个公式作为起点:

sort ascending/descending vba excel

升序/降序排序vba excel

It only sorts one column and in descending order. I am having trouble finding more examples.

它只对一列进行降序排序。我无法找到更多示例。

LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

If (.Range("D2").Value > .Range("D" & CStr(LastRow))) Then
    xlSort = xlAscending
End If

.Range("D2:D" & LastRow).Sort Key1:=.Range("D2"), Order1:=xlSort, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

回答by JC11

Here is the answer:

这是答案:

.Range("D1") = "Index"
.Columns("A:F").Sort key1:=Range("D2"), _
order1:=xlAscending, Header:=xlYes

Thanks to simoco's comment!

感谢 simoco 的评论!