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



我试图在VBA excel文件中按升序排序列D。我想为A-F列中的所有值展开选择。

我用这个公式作为起点:

排序升序/降序vba excel

只对一列进行降序排序。我很难找到更多的例子。

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

答案如下:

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

感谢simoco的评论!

最新更新