• Source
    1. Option Explicit
    2. '人名構造体
    3. Type PERSON
    4. name As String '名前
    5. count As Long '件数
    6. End Type
    7.  
    8. Public Sub 並べ替え()
    9. Dim persons() As PERSON '人名テーブル(可変)
    10. Dim dicT As Object '連想配列 キー:人名 値:シリアル番号
    11. Dim seqNo As Long: seqNo = 0 '通番
    12. Dim ws As Worksheet '処理対象シート
    13. Dim lastrow As Long '最終行
    14. Dim wrow As Long '作業用行
    15. Dim name As String '名前
    16. Dim count As Long '件数
    17. Dim i As Long
    18. Dim j As Long
    19. Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
    20. Set ws = ActiveSheet 'アクティブシートを処理対象とする
    21. lastrow = ws.Cells(Rows.count, "K").End(xlUp).Row 'K列の最終行取得
    22. 'K列の2行以降を読み込む
    23. For wrow = 2 To lastrow
    24. name = ws.Cells(wrow, "K").Value
    25. If dicT.exists(name) = False Then
    26. '最初に出現した名前の場合
    27. '人名テーブルに登録
    28. i = seqNo
    29. ReDim Preserve persons(i)
    30. persons(i).name = name
    31. persons(i).count = 1
    32. 'テーブル添え字を連想配列に登録
    33. dicT(name) = seqNo
    34. seqNo = seqNo + 1
    35. Else
    36. '2回目以降の出現の場合
    37. 'その名前のシリアル番号取得
    38. i = dicT(name)
    39. '件数に1加算
    40. persons(i).count = persons(i).count + 1
    41. End If
    42. Next
    43. '人名テーブルをK列へ出力する
    44. wrow = 2
    45. For i = 0 To seqNo - 1
    46. '当該添え字の名前と件数を取得
    47. name = persons(i).name
    48. count = persons(i).count
    49. '名前を件数分書き込む
    50. For j = 1 To count
    51. ws.Cells(wrow, "K").Value = name
    52. wrow = wrow + 1
    53. Next
    54. Next
    55. MsgBox ("完了")
    56. End Sub
    57.