|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub test2() '还来一个灌一灌
Dim dict As Object
Dim ar, i As Long, j As Long, k As Long, p As Long, s As String
k = 1
Set dict = CreateObject("Scripting.Dictionary")
ar = Range("B1", Cells(Rows.Count, "A").End(xlUp))
ar(k, 1) = "次数": ar(k, 2) = "数量"
For i = 2 To UBound(ar)
s = Trim(ar(i, 1))
If Len(s) Then
If Not dict.Exists(s) Then
k = k + 1
ar(k, 1) = 1
dict.Add s, k
Else
p = dict(s)
ar(p, 1) = ar(p, 1) + 1
End If
End If
Next
Set dict = Nothing
QuickSort ar, 2, k, UBound(ar, 2) - 1
s = vbNullString
j = 1
For i = 2 To k
If Trim(ar(i, 1)) <> s Then
s = Trim(ar(i, 1))
j = j + 1
ar(j, 1) = s
ar(j, 2) = 1
Else
ar(j, 2) = ar(j, 2) + 1
End If
Next
With Range("J1")
.CurrentRegion.Clear
.Resize(j, UBound(ar, 2)) = ar
End With
End Sub
Function QuickSort(ar, L1 As Long, U1 As Long, pos As Long)
Dim up As Long, dn As Long, pivot As Long, swap As Long
up = L1
dn = U1
pivot = ar((L1 + U1) \ 2, pos)
While up <= dn
Do
If ar(up, pos) < pivot Then up = up + 1 Else Exit Do
Loop While up < U1
Do
If ar(dn, pos) > pivot Then dn = dn - 1 Else Exit Do
Loop While dn > L1
If up < dn Then
swap = ar(up, 1): ar(up, 1) = ar(dn, 1): ar(dn, 1) = swap
up = up + 1: dn = dn - 1
Else
If up = dn Then up = up + 1: dn = dn - 1
End If
Wend
If up < U1 Then QuickSort ar, up, U1, pos
If dn > L1 Then QuickSort ar, L1, dn, pos
End Function |
|