除了 Alistair 对数据透视表的建议,我也有这个
打印到页面
Dim Str As String
Set Rng = range(range("A1"), range("A" & Rows.Count).End(xlUp))
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = 1
For Each Dn In Rng
If Not Dic.exists(Dn.Value) Then
Set Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
End If
If Not Dic(Dn.Value).exists(Dn.Offset(, 3).Value) Then
Dic(Dn.Value).Add (Dn.Offset(, 3).Value), 1
Else
Q = Dic(Dn.Value).Item(Dn.Offset(, 3).Value)
Q = Q + 1
Dic(Dn.Value).Item(Dn.Offset(, 3).Value) = Q
End If
Next Dn
Dim C As Integer
Dim Ac As Integer
C = 4
For Each k In Dic.Keys
C = C + 1
Ac = 1
Cells(Ac, C) = k
For Each p In Dic(k)
Ac = Ac + 1
Cells(Ac, C) = p & " (" & Dic(k).Item(p) & ")"
Next p
Next k
End Sub
在 MessageBox 中显示
Sub Report()
Dim Dn As range
Dim Rng As range
Dim Dic As Object
Dim Q As Variant
Dim k As Variant
Dim p As Variant
Dim Str As String
Set Rng = range(range("A2"), range("A" & Rows.Count).End(xlUp))
Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = 1
For Each Dn In Rng
If Not Dic.exists(Dn.Value) Then
Set Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
End If
If Not Dic(Dn.Value).exists(Dn.Offset(, 1).Value) Then
Dic(Dn.Value).Add (Dn.Offset(, 1).Value), 1
Else
Q = Dic(Dn.Value).Item(Dn.Offset(, 1).Value)
Q = Q + 1
Dic(Dn.Value).Item(Dn.Offset(, 1).Value) = Q
End If
Next Dn
For Each k In Dic.Keys
Str = Str & k & " :- "
For Each p In Dic(k)
Str = Str & p & " (" & Dic(k).Item(p) & ") , "
Next p
Str = Str & Chr(10)
Next k
MsgBox Str
End Sub