Range.Sort メソッド (Excel) | Microsoft Learn

この記事では、ExcelのRangeオブジェクトのソート機能について説明しています。ソートの構文や、ソートフィールド(Key1、Key2、Key3)やソート順序(Order1、Order2、Order3)、ヘッダーの有無、選択肢、オリエンテーションなどのオプションが含まれています。例として、A列のセルの色のインデックスを取得し、C列にその値を記入してから、C列のデータに基づいて行をソートする方法が示されています。最後に、サポートとフィードバックの案内があります。

Excel VBAのSortメソッドについて

Excel VBAにおけるSortメソッドは、特定の範囲の値をソート(整列)するために使用されます。この記事では、Sortメソッドの構文やパラメータ、実際の使用例などについて詳しく解説します。

構文

expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)
  • expression: Rangeオブジェクトを表す変数です。

パラメータ

以下はSortメソッドで使用できる主要なパラメータです。

名前 必須/任意 データ型 説明
Key1 任意 Variant 最初のソートフィールドを指定します。範囲名またはRangeオブジェクトで指定します。
Order1 任意 XlSortOrder Key1で指定した値のソート順序を決定します。
Key2 任意 Variant 2番目のソートフィールド。ピボットテーブルをソートする際には使用できません。
Type 任意 Variant ピボットテーブル内でどのタイプの要素をソートするかを指定します。
Order2 任意 XlSortOrder Key2で指定した値のソート順序を決定します。
Header 任意 XlYesNoGuess 最初の行がヘッダー情報を含むかどうかを指定します。
MatchCase 任意 Variant 大文字と小文字を区別するかどうかを指定します。

戻り値

Sortメソッドは、Variant型の値を返します。

使用例

以下は、ExcelのVBAによって色を基準に値をソートする例です。

Sub ColorSort()
   ' 変数の設定と画面更新の停止
   Dim iCounter As Integer
   Application.ScreenUpdating = False

   ' 列Aの各セルの色インデックス値を列Cに格納
   For iCounter = 2 To 55
      Cells(iCounter, 3) = Cells(iCounter, 1).Interior.ColorIndex
   Next iCounter

   ' 列Cのデータに基づいて行をソート
   Range("C1") = "Index"
   Columns("A:C").Sort key1:=Range("C2"), order1:=xlAscending, header:=xlYes

   ' 列Cの一時的なソート値をクリアし、画面更新を再開
   Columns(3).ClearContents
   Application.ScreenUpdating = True
End Sub

この例では、列Aの各セルの色インデックスを列Cにコピーし、その後列Cの値に基づいて行をソートしています。最後に、一時的なデータを列Cからクリアし、画面表示を再度有効にしています。

サポートとフィードバック

Office VBAやこのドキュメントに関する質問やフィードバックがある場合は、Office VBAのサポートとフィードバックを参照してください。

このように、Sortメソッドを利用することで、Excel内でのデータ処理を効率的に行うことができます。データの整列はさまざまなレポートや分析において重要なステップですので、ぜひ活用してみてください。

————-

Range.Sort method (Excel) | Microsoft Learn

Source link

The article provides an overview of the Sort method for sorting a range of values in Excel using VBA (Visual Basic for Applications). The syntax for the method is detailed, along with its parameters, return value, and a sample code example.

Key Points:

  • Syntax: The Sort method follows this structure:
    expression.Sort (Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3).
    Here, expression represents a Range object.

  • Parameters:

    • Key1, Key2, Key3: These optional parameters specify the fields to sort by.
    • Order1, Order2, Order3: These optional parameters define the sort order (ascending or descending) for each key.
    • Header: Indicates whether the first row contains header information (default is xlNo).
    • MatchCase: If True, sorts in a case-sensitive manner.
    • Orientation: Specifies whether sorting is done by rows or columns.
    • SortMethod and DataOptions: Provide additional sorting preferences.
  • Return Value: The method returns a Variant.

Example Code:

An example code snippet demonstrates how to sort a range based on the color index of cells in column A. The code copies color index values to column C, sorts the range by this data, and then clears out the temporary values used for sorting.

Support Information:

The article includes links for further support and feedback related to Office VBA.

Overall, this article is a resource for users wanting to implement sorting functionality in Excel using VBA.

関連記事

コメント

この記事へのコメントはありません。