Range.Sort Method (Excel)

Sorts a range of values.

Syntax

.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)

A variable that represents a Range object.

Parameters

Name

Required/Optional

Data Type

Description

Key1

선택

Variant

Specifies the first sort field, either as a range name (String) or Range object; determines the values to be sorted.

Order1

선택

XlSortOrder

Determines the sort order for the values specified in Key1.

Key2

선택

Variant

Second sort field; cannot be used when sorting a pivot table.

Type

선택

Variant

Specified which elements are to be sorted.

Order2

선택

XlSortOrder

Determines the sort order for the values specified in Key2.

Key3

선택

Variant

Third sort field; cannot be used when sorting a pivot table.

Order3

선택

XlSortOrder

Determines the sort order for the values specified in Key3.

Header

선택

XlYesNoGuess

Specifies whether the first row contains header information. xlNo is the default value; specify xlGuess if you want Excel to attempt to determine the header.

OrderCustom

선택

Variant

Specifies a one-based integer offset into the list of custom sort orders.

MatchCase

선택

Variant

Set to True to perform a case-sensitive sort, False to perform non-case sensitive sort; cannot be used with pivot tables.

Orientation

선택

XlSortOrientation

Specifies if the sort should be in acending or decending order.

SortMethod

선택

XlSortMethod

Specifies the sort method.

DataOption1

선택

XlSortDataOption

Specifies how to sort text in the range specified in Key1; does not apply to pivot table sorting.

DataOption2

선택

XlSortDataOption

Specifies how to sort text in the range specified in Key2; does not apply to pivot table sorting.

DataOption3

선택

XlSortDataOption

Specifies how to sort text in the range specified in Key3; does not apply to pivot table sorting.

Return Value

Variant

Example

Sample code provided by: Holy Macro! Books, Holy Macro! It’s 2,500 Excel VBA Examples | About the Contributor

This example gets the value of the color of a cell in column A using the ColorIndex property, and then uses that value to sort the range by color.

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

About the Contributor

Holy Macro! Books에서는 Microsoft Office 사용자를 위한 재미있는 서적을 다수 출판하고 있습니다. 전체 카탈로그는 MrExcel.com(영문일 수 있음)에서 확인할 수 있습니다.

참고 항목

개념

Range Object

Range Object Members