排序值的範圍。
語法
運算式。排序 (Key1、 Order1、 Key2、 Type、 Order2、 Key3、 Order3、 Header、 OrderCustom、 MatchCase、 Orientation、 SortMethod、 DataOption1、 DataOption2、 DataOption3)
expression 代表 Range 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| Key1 | 選用 | Variant | 指定第一個排序欄位, (字 串) 或範圍 物件做為範圍名稱;決定要排序的值。 |
| Order1 | 選用 | XlSortOrder | 在 Key1 中決定指定值的順序排序。 |
| Key2 | 選用 | Variant | 第二個排序欄位;無法用於排序樞紐分析表。 |
| 類型 | 選用 | Variant | 指定要在樞紐分析表中排序的元素類型。 指定 xLSortLabels,以依據樞紐分析表範圍之列/列標籤區域中的運算式範圍所針對的標籤排序,或者當 Key1 另外使用 R1C1 標記法以 [值] 或 [子/總計] 區域中的儲存格為目標時,依據 xLSortValues 排序。 |
| Order2 | 選用 | XlSortOrder | 決定 Key2 中指定值的排序順序。 |
| Key3 | 選用 | Variant | 第三個排序欄位;無法用於排序樞紐分析表。 |
| Order3 | 選用 | XlSortOrder | 決定在 Key3中所指定值的順序排序。 |
| 頁首 | 選用 | XlYesNoGuess | 指定第一列是否包含標題資訊。 xlNo 是預設值,若要 Excel 試著判斷標題,請指定 xlGuess 。 |
| OrderCustom | 選用 | Variant | 指定自訂排序順序清單中以一起始的整數位移。 |
| MatchCase | 選用 | Variant | 設定為 True 以執行區分大小寫的排序,設定為 False 以執行不區分大小寫的排序;無法與樞紐分析表搭配使用。 |
| Orientation | 選用 | XlSortOrientation | 指定是否應以列 (預設值) 或欄進行排序。 將 xlSortColumns 值設定為 1,以依欄排序。 將 xlSortRows 值設定為 2,以依列排序 (這是預設值) 。 |
| SortMethod | 選用 | XlSortMethod | 指定排序方向。 |
| DataOption1 | 選用 | XlSortDataOption | 指定如何排序 Key1 中指定的範圍內的文字;不適用於樞紐分析表排序。 |
| DataOption2 | 選用 | XlSortDataOption | 指定如何排序 Key2 中指定的範圍內的文字;不適用於樞紐分析表排序。 |
| DataOption3 | 選用 | XlSortDataOption | 指定如何排序 Key3 中指定範圍內的文字;不適用於樞紐分析表排序。 |
傳回值
Variant
範例
此範例使用 ColorIndex 屬性取得欄 A 中儲存格的色彩值,然後使用該值來依色彩排序範圍。
Sub ColorSort()
'Set up your variables and turn off screen updating.
Dim iCounter As Integer
Application.ScreenUpdating = False
'For each cell in column A, go through and place the color index value of the cell in column C.
For iCounter = 2 To 55
Cells(iCounter, 3) = _
Cells(iCounter, 1).Interior.ColorIndex
Next iCounter
'Sort the rows based on the data in column C
Range("C1") = "Index"
Columns("A:C").Sort key1:=Range("C2"), _
order1:=xlAscending, header:=xlYes
'Clear out the temporary sorting value in column C, and turn screen updating back on.
Columns(3).ClearContents
Application.ScreenUpdating = True
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。