HOME > 即効テクニック > Excel VBA > 文字列操作関連のテクニック > n文字目から始まる文字列でソートする 

n文字目から始まる文字列でソートする |Excel VBA

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

n文字目から始まる文字列でソートする 

(Excel 97/2000/2002/2003/2007/2010)

次のサンプルは、タイトル行のあるデータを、A列の4文字目以降の文字列でソートします。

Sub Sample()
    Const START As Long = 4  '4文字目以降で並べ替え
    Dim c As Range
    
    Application.ScreenUpdating = False
    
    '作業列を追加
    Columns(1).Insert
    
    For Each c In Range(Range("B2"), Cells(Rows.Count, 2).End(xlUp))
        With c
            .Offset(0, -1).Value = Mid(.Value, START)
        End With
    Next c
    
    '並べ替え
    Cells.Sort Range("A1"), xlAscending, Header:=xlYes
    
    '作業列を削除
    Columns(1).Delete
    
    Application.ScreenUpdating = True
End Sub