Sub XYZ()
Dim sArr(), Arr(), tmp, Res(), iKey$, iKey2$
Dim sRow&, i&, k&, iR&, j&, c&, jC&, t&
Const n = 10 'Gioi han So luong loai du lieu A,B ...
With Sheets("Sheet1")
i = Range("A" & Rows.Count).End(xlUp).Row
If i < 1 Then MsgBox "Khong co du lieu": Exit Sub
sArr = Range("A2:B" & i).Value
End With
sRow = UBound(sArr)
ReDim Arr(0 To n, 1 To 2) 'Mang du lieu tung Lop
ReDim Res(1 To sRow, 1 To 3)
With CreateObject("scripting.dictionary")
For i = 1 To sRow
iKey = sArr(i, 2)
iKey2 = iKey & "|" & sArr(i, 1)
If .exists(iKey) = False Then
k = k + 1
.Add iKey, k
Res(k, 1) = iKey
Res(k, 3) = Arr 'Mang du lieu tung Lop
End If
iR = .Item(iKey)
tmp = Res(iR, 3) 'Mang du lieu tung Lop
j = tmp(0, 1)
If .exists(iKey2) = False Then
j = j + 1 ' Thu tu du kieu
.Add iKey2, j
tmp(j, 1) = sArr(i, 1)
End If
jC = .Item(iKey2) ' Thu tu du kieu
tmp(jC, 2) = tmp(jC, 2) + 1 'Dem du lieu
tmp(0, 1) = j
Res(iR, 3) = tmp 'Mang du lieu tung Lop
Next i
End With
For i = 1 To k 'Xep thu tu
tmp = Res(i, 3)
sRow = tmp(0, 1)
ReDim Arr(1 To sRow)
For j = sRow To 1 Step -1
t = tmp(j, 2)
jC = 0
For c = 1 To sRow
If tmp(c, 2) >= t Then jC = jC + 1
Next c
For c = j + 1 To sRow
If tmp(c, 2) = t Then jC = jC - 1
Next c
Arr(jC) = tmp(j, 1)
Next j
Res(i, 2) = Join(Arr, ",")
Next i
Sheets("Sheet1").Range("F2").Resize(k, 2) = Res
End Sub