Option base 1
Sub Macro1()
'変数の定義
Dim last As Long
Dim i As Long, j As Long
Dim n As Variant, m As Variant
'最下行を求めてlastに代入
last = Range("A1").End(xlDown).Row
'必要なだけの配列を用意
ReDim ary(last) As Variant
ReDim item(last) As Variant
’配列に2~最下行のデータを格納
For i = 2 To last
ary(i) = Cells(i, 1) & "#" & Cells(i, 2)
item(i) = Cells(i, 3)
Next
’番号とコンテナを連結した値でソート
For i = 2 To last - 1
For j = i + 1 To last
If ary(i) > ary(j) Then
n = ary(i)
ary(i) = ary(j)
ary(j) = n
n = item(i)
item(i) = item(j)
item(j) = n
End If
Next
Next
’E列~G列に結果を代入
m = ary(2)
n = item(2)
j = 2
For i = 3 To last
If m <> ary(i) Then
Cells(j, 5) = Left(m, InStr(1, m, "#") - 1)
Cells(j, 6) = Mid(m, InStr(1, m, "#") + 1, Len(m))
Cells(j, 7) = n
m = ary(i)
n = item(i)
j = j + 1
Else
n = n & "," & item(i)
End If
Next
Cells(j, 5) = Left(m, InStr(1, m, "#") - 1)
Cells(j, 6) = Mid(m, InStr(1, m, "#") + 1, Len(m))
Cells(j, 7) = n
End Sub
お礼
連想配列を使う方法ですね。 できました! ありがとうございます。