💻
ExcelVBAで特定のデータ範囲をソートする
ソート用サブプロシージャ
'特定のデータ範囲をソートする
'データ範囲はEndステートメントで確定
'引数でKeyを渡さないといけない
'Keyは”A1”の形式で渡す(文字列)
'1行目はタイトル行であると想定
Private Sub S_SortRange(ByVal arg_sortSheet As Worksheet, ByVal arg_startRow As Long, arg_startColumn As Long, ByVal arg_sortKey As String)
Dim sortRange As Range
Dim lastRow, lastColumn As Long
arg_sortSheet.Activate
lastRow = Cells(arg_startColumn, 1).End(xlDown).Row
lastColumn = Cells(arg_startRow, 1).End(xlToRight).Column
Set sortRange = Range(Cells(2, 1), Cells(lastRow, lastColumn))
sortRange.Sort Key1:=Range(arg_sortKey), order1:=xlAscending
End Sub
ソート用サブプロシージャを呼び出す
Sub S_Main()
Dim sortSheetName As String
Dim sortSheet As Worksheet
Dim startRow, startColumn As Long
Dim sortKey As String
sortSheetName = "Zenn"
Set sortSheet = ActiveWorkbook.Worksheets(sortSheetName)
startRow = 1
startColumn = 1
sortKey = "A1"
Call S_SortRange(sortSheet, startRow, startColumn, sortKey)
End Sub
'特定のデータ範囲をソートする
'データ範囲はEndステートメントで確定
'引数でKeyを渡さないといけない
'Keyは”A1”の形式で渡す(文字列)
'1行目はタイトル行であると想定
Private Sub S_SortRange(ByVal arg_sortSheet As Worksheet, ByVal arg_startRow As Long, arg_startColumn As Long, ByVal arg_sortKey As String)
Dim sortRange As Range
Dim lastRow, lastColumn As Long
arg_sortSheet.Activate
lastRow = Cells(arg_startColumn, 1).End(xlDown).Row
lastColumn = Cells(arg_startRow, 1).End(xlToRight).Column
Set sortRange = Range(Cells(2, 1), Cells(lastRow, lastColumn))
sortRange.Sort Key1:=Range(arg_sortKey), order1:=xlAscending
End Sub
Discussion