(Excel) 对对象进行排序

代表数据区域的排序方式。

示例

以下过程在活动工作表的某个区域中生成数据并对其进行排序。

Sub SortData() 
 
 'Building data to sort on the active sheet. 
 Range("A1").Value = "Name" 
 Range("A2").Value = "Bill" 
 Range("A3").Value = "Rod" 
 Range("A4").Value = "John" 
 Range("A5").Value = "Paddy" 
 Range("A6").Value = "Kelly" 
 Range("A7").Value = "William" 
 Range("A8").Value = "Janet" 
 Range("A9").Value = "Florence" 
 Range("A10").Value = "Albert" 
 Range("A11").Value = "Mary" 
 MsgBox "The list is out of order. Hit Ok to continue...", vbInformation 
 
 'Selecting a cell within the range. 
 Range("A2").Select 
 
 'Applying sort. 
 With ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort 
 .SortFields.Clear 
 .SortFields.Add Key:=Range("A2:A11"), _ 
 SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 
 .SetRange Range("A1:A11") 
 .Header = xlYes 
 .MatchCase = False 
 .Orientation = xlTopToBottom 
 .SortMethod = xlPinYin 
 .Apply 
 End With 
 MsgBox "Sort complete.", vbInformation 
 
End Sub

方法

属性

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。