简体   繁体   English

VBA代码按升序排序Excel列并展开选择?

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

I am attempting to sort column D in ascending order in an excel file with VBA. 我试图在带有VBA的excel文件中按升序对列D进行排序。 I would like to expand the selection for all values in Column AF. 我想扩展列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

Here is the answer: 这是答案:

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

Thanks to simoco's comment! 感谢simoco的评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM