2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ExcelのVBAで複数列のSORTを3行で記述する

Last updated at Posted at 2024-12-17

Rangeオブジェクトを並び替える

左のデータを右のようにソートしたいとします
image.png

sort関数の第2引数にsortしたい列をバリアント配列で渡すことにより複数列を指定できます

With Range("A2:C16")
    .Value = WorksheetFunction.sort(.Value, Array(2, 1))
End With

降順にsortしたい場合は第3引数に1昇順,-1降順をバリアント配列で渡すことにより指定できます

With Range("A2:D16")
    .Value = WorksheetFunction.sort(.Value, Array(2, 1), Array(-1, 1))
End With

セル関数の、SORT関数で複数列をソートする方法を、VBAのコードで実現しています

参考までにユーザ定義の1次元配列を並び替えるには

    配列 = Array(5, 4, 3, 2, 1)
    '第3引数がTrueで並び換え基準に列方向を指定
    配列 = WorksheetFunction.Sort(配列, , , True)
    '注意:代入先の変数の型はバリアント型でないとダメ
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?