Home > 即効テクニック > Excel VBA > 文字列操作関連のテクニック > 特定の列のデータを特定番目から始まる文字列をキーにソートする

即効テクニック

文字列操作関連のテクニック

特定の列のデータを特定番目から始まる文字列をキーにソートする

(Excel 97/2000/)
サンプルマクロは、タイトル行のあるA列のデータを4文字目以降の文字列をキーにソートします。
Sub Sample()

    Dim myStart As Integer
    Dim myCell As Range
    
    myStart = 4
    
    Application.ScreenUpdating = False
    
    Columns(1).Insert
    For Each myCell In _
        Range(Range("B2"), Cells(Rows.Count, 2).End(xlUp))
        With myCell
            .Offset(, -1).Value = Mid(.Value, myStart)
        End With
    Next
    Cells.Sort Range("A1"), xlAscending, header:=xlYes
    Columns(1).Delete
    
    Application.ScreenUpdating = True

End Sub