2014-03-24 77 views
2

我正尝试使用VBA在Excel文件中按升序对列D进行排序。我想扩展A-F列中所有值的选择。按升序排列Excel列并扩展选择的VBA代码?

我用这个公式为出发点:

sort ascending/descending 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 
+4

http://msdn.microsoft.com/en-us/library/office/ff840646(v=office.15).aspx –

回答

10

下面是答案:

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

由于simoco的评论!